Ankita Ankita - 5 months ago 23
Python Question

mongo query in python

I have a command line argument user_id as --uid.

I want to access the record of that particular user_id from another collection "student_details" in python.

As I am new to mongoDB, I would like to get the answer for my particular query

users={
_id :Object(###),
name:"",
standard:""
..
..
..
}


student_details={
_id:Object(###),
user_id:"", -----> referenced
..
}


I tried doing this,

collection = db['student_details']
query = "%s" % option.uidfile
results = collection.find({"user_id":query})

print results


ouput:

<pymongo.cursor.Cursor object at 0x95c910c>


Thanks in advance.

Answer

You should take a look at the tutorial here
http://api.mongodb.org/python/2.0/tutorial.html
But if you had collection "student_details" opened in python you would query:

collection.find({"userID" : user_id})
Comments