Database Reference
In-Depth Information
{
u'Status': u'In use',
u'Tags': [u'Laptop', u'Development', u'In Use'],
u'ItemNumber': u'3456TFS',
u'Location': {
u'Department': u'Development',
u'Building': u'2B',
u'Floor': 12,
u'Owner': u'Walker, Jan',
u'Desk': 120103
},
u'_id': ObjectId('4c57234c4abffe0e0c000002'),
u'Type': u'Laptop'
}
Using indexes can help you significantly increase performance speed when the size of your collections keeps
growing (see Chapter 10 for more details on performance tuning).
Refining Queries with Conditional Operators
You can use conditional operators to refine your query. MongoDB includes more than half a dozen conditional
operators accessible through PyMongo; these are identical to the conditional operators you've seen in the previous
chapters. The following sections walk you through the conditional operators available in Python, as well as how you
can use them to refine your MongoDB queries in Python.
Using the $lt, $gt, $lte, and $gte Operators
Let's begin by looking at the $lt , $gt , $lte , and $gte conditional operators. You can use the $lt operator to search
for any numerical information that is less than n . The operator takes only one parameter: the number n , which
specifies the limit. The following example finds any entries that have a desk number lower than 120102. Note that the
comparison value itself is not included:
>>> for doc in collection.find({"Location.Desk" : {"$lt" : 120102} }):
... doc
...
{
u'Status': u'In use',
u'Tags': [u'Laptop', u'Development', u'In Use'],
u'ItemNumber': u'1234EXD',
u'Location': {
u'Department': u'Development',
u'Building': u'2B',
u'Floor': 12,
u'Owner': u'Anderson, Thomas',
u'Desk': 120101
},
u'_id': ObjectId('4c57207b4abffe0e0c000000'),
u'Type': u'Laptop'
}
 
Search WWH ::




Custom Search