Database Reference
In-Depth Information
{
u'ItemNumber': u'1234EXD',
u'_id': ObjectId('4c57207b4abffe0e0c000000'),
u'Location': {
u'Owner': u'Anderson, Thomas'
}
}
{
u'ItemNumber': u'2345FDX',
u'_id': ObjectId('4c57234c4abffe0e0c000001'),
u'Location': {
u'Owner': u'Smith, Simon'
}
}
{
u'ItemNumber': u'3456TFS',
u'_id': ObjectId('4c57234c4abffe0e0c000002'),
u'Location': {
u'Owner': u'Walker, Jan'
}
}
I suspect you'll agree that this approach to specifying criteria is quite handy.
Simplifying Queries with sort(), limit(), and skip()
The sort() , limit() , and skip() functions make implementing your queries much easier. Individually, each of
these functions has its uses, but combining them makes them even better and more powerful. You can use the sort()
function to sort the results by a specific key; the limit() function to limit the total number of results returned; and the
skip() function to skip the first n items found before returning the remainder of the documents that match your query.
Let's look at a set of individual examples, beginning with the sort() function. To save some space, the following
example includes another parameter to ensure that only a few fields are returned:
>>> for doc in collection.find ({'Status' : 'In use'},
... fields={'ItemNumber' : True, 'Location.Owner' : True}).sort('ItemNumber'):
... doc
...
{
u'ItemNumber': u'1234EXD',
u'_id': ObjectId('4c57207b4abffe0e0c000000'),
u'Location': {
u'Owner': u'Anderson, Thomas'
}
}
{
u'ItemNumber': u'2345FDX',
u'_id': ObjectId('4c57234c4abffe0e0c000001'),
u'Location': {
u'Owner': u'Smith, Simon'
}
}
 
Search WWH ::




Custom Search