Once you got a collection
object, queries use the same syntax as in the mongo shell. Some slight differences are:
every key must be enclosed in brackets. For example:
db.find({frequencies: {$exists: true}})
becomes in pymongo
(note the True
in uppercase):
db.find({"frequencies": { "$exists": True }})
objects such as object ids or ISODate
are manipulated using python classes. PyMongo uses its own
ObjectId
class to deal with object ids, while dates use the standard datetime
package. For example,
if you want to query all events between 2010 and 2011, you can do:
from datetime import datetime
date_from = datetime(2010, 1, 1)
date_to = datetime(2011, 1, 1)
db.find({ "date": { "$gte": date_from, "$lt": date_to } }):