1

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 from 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?

1 Answer 1

1

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}})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! For some reason when I was using the variable directly in the dict I wasn't getting the quoting I needed. Your solution worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.