1

Consider the have the following collection:

db.collection.insert({id:1, value:1})
db.collection.insert({id:2, value:2})
db.collection.insert({id:3, value:3})

Now, I have a file which has a list of ids. I can read the file and store the id in a variable. How do I now query the collection to get the details of that particular id:

f=open("file.txt")
for line in f:
  idTemp=line.strip()
  db.collection.find_one({"id":idTemp}) #This does not work

1 Answer 1

4

id is an int, but anything read directly from a file is a string. Convert it to int first:

idTemp = int(line.strip())
Sign up to request clarification or add additional context in comments.

1 Comment

Can you help me with this, stackoverflow.com/questions/37707033/…

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.