In order to produce a list view on a web page, or to pull more than a single record out of MongoDB, say, for analysis, you will need to query for multiple records.
PyMongo provides a find()
function that we can use to search for multiple documents that match a given criteria, returning a cursor instance that we can iterate over.
from pymongo import MongoClient client = MongoClient('localhost', 27017) db = client.pythonbicookbook accidents = db.accidents # Retrieve all records where the accident happened on a Friday data = accidents.find({"Day_of_Week": 7}) # Show a count for the result data.count() # returns 896218
The find()
function is a powerful method for filtering a result set to get only the records that you want. You can search on a single field, as we have in this recipe, or search on multiple fields. In addition, you can limit your results to a subset of the available fields using the projection argument. You can also limit the number of results provided in order to get a top
count.