2

Am trying to find documents from the mongo collection using the following query. db.collection_name.find({"id" : Id}) where Id is the variable am getting as input. But it doesn't work. If I hard code the value like this db.collection_name.find({"id" : "1a2b"}) it works. "id" is of string type and am using pymongo to access mongo DB.

code :

client = MongoClient("localhost:27017")                
db = client['sample_database']
Id = raw_input("enter id") 
cursor = db.collection_name.find({"id" : Id})
6
  • 1
    can you please share all of your code before calling find()? Commented Jun 8, 2016 at 15:44
  • I have edited with all of my code Commented Jun 8, 2016 at 15:49
  • 1
    Where did you define your Id? I would expect something like this; Id = str(post_id) Commented Jun 8, 2016 at 15:51
  • Possible duplicate of How do I use pymongo to connect to an existing document collection/db? Commented Jun 8, 2016 at 15:54
  • Am getting Id as input Commented Jun 8, 2016 at 15:54

2 Answers 2

3

Try str();

Id = str(raw_input("enter id"))
cursor = db.collection_name.find({"id" : Id})
Sign up to request clarification or add additional context in comments.

Comments

2

This may help you..in python3 it is working..

Id = raw_input("enter id: ") 
cursor = db.collection_name.find({"id" : Id})
for i in cursor:
    print(i)

there is no require to convert raw_input() to string,Because raw_input() is already get input from user as string..

Comments

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.