开发者

Using JSON in Python to send query to MongoDB

I'm trying to create the following query in Python for a connection I have to MongoDB.

db.deal.find({"id":"11223|14589"})

By using the writing the following code:

import json

unique_id = "11223|14589"

query = json.dumps({"id":"%s"}) %(unique_id)

Unfortunately, this creates a string, and the results I don't get any returned results fr开发者_开发问答om MongoDB. Querying manually using the string that is created in variable query actually returns results. Anyone know what I might do to get this working from pymongo?


Not sure what you're trying to achieve here.

First of all, you should not have to serialize anything into JSON. Especially not replace parts of the string after it's serialized.

Second, is unique_id one value that you're looking for? If so, just pass it to the query:

unique_id = '11223|14589'
foo.find({'id': unique_id})

Or are these two values you'd like to search for?

unique_id = '11223|14589'
ids = unique_id.split('|') # ['11223', '14589']
foo.find({'id': {'$in': ids}})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜