Database Reference
In-Depth Information
u'Department': u'Storage',
u'Building': u'2B'
},
u'_id': ObjectId('4c5973554abffe0e0c000005')
}
Similarly, you can see its three most recent locations by making the integer value negative:
>>> collection.find_one({'ItemNumber' : '6789SID'}, {'PreviousLocation' : {'$slice' : -3} })
{
u'Status': u'Not used',
u'PreviousLocation': [u'120108', u'120109', u'120110'] ,
u'Tags': [u'Chair', u'Not used', u'Storage'],
u'ItemNumber': u'6789SID',
u'Location': {
u'Department': u'Storage',
u'Building': u'2B'
},
u'_id': ObjectId('4c5973554abffe0e0c000005')
}
Or, you could skip the first five locations for the chair and limit the number of results returned to three
(pay special attention to the brackets here):
>>> collection.find_one({'ItemNumber' : '6789SID'}, {'PreviousLocation' : {'$slice' : [5, 3] } })
{
u'Status': u'Not used',
u'PreviousLocation': [u'120105', u'120106', u'120107'] ,
u'Tags': [u'Chair', u'Not used', u'Storage'],
u'ItemNumber': u'6789SID',
u'Location': {
u'Department': u'Storage',
u'Building': u'2B'
},
u'_id': ObjectId('4c5973554abffe0e0c000005')
}
You probably get the idea. This example might seem a tad unusual, but inventory control systems often veer
into the unorthodox; and the $slice operator is intrinsically good at helping you account for unusual or complex
circumstances. For example, the $slice operator might prove an especially effective tool for implementing the paging
system for a website's Comments section.
Conducting Searches with Regular Expressions
One useful tool for conducting searches is the regular expression . The default regular expression module for Python is
called re . Performing a search with the re module requires that you first load the module, as in this example:
>>> import re
 
Search WWH ::




Custom Search