What You’ll Learn in This Hour:
Paging documents from a large dataset in Python
Limiting which fields are returned from documents in Python
Using methods to generate lists of distinct field values in documents from a collection in Python
Implementing grouping from Python to group documents and build a return dataset
Applying an aggregation pipeline to build a dataset from documents in a collection from Python
In this hour, you continue your look at the Python MongoDB driver and how to implement it to retrieve data in your Python applications. This hour takes you through the process of using Python applications to limit the return results by limiting the number of documents returned, limiting the fields returned, and paging through large sets.
This hour also covers how to implement distinct, grouping, and aggregation operations from a Python application. These operations enable you to process data on the server before returning it to the Python application, reducing the amount of data sent and the work required in the application.
When finding documents on larger databases with more complex documents, you often want to limit what is being returned in requests, to reduce the impact on the network, memory on both the server and client, and more. You can limit the result sets that match a specific query in three ways. You can simply accept only a limited number of documents, you can limit the fields that are returned, or you can page the results and get them in paged chunks.
The simplest method of limiting the amount of data returned in a find()
or other query request is to use the limit()
method on the Cursor
object returned by the find()
operation. The limit()
method limits the Cursor
object so that it returns only a fixed number of items. This can save you from accidentally retrieving more objects than your application can handle.
For example, the following Python code displays only the first 10 documents in a collection, even though there could be thousands:
cursor = wordsColl.find()
cursor.limit(10)
for word in cursor:
print (word)
Another extremely effective method of limiting the resulting data when retrieving documents is to limit which fields are returned. Documents might have many different fields that are useful in some circumstance but not in others. Consider which fields to include when retrieving documents from the MongoDB server, and request only the ones necessary.
To limit the fields returned from the server in a find()
operation on a Collection
object, you can use the fields
parameter. This parameter is a Dictionary
that contains the files with a value of true
to include or false
to exclude.
For example, to exclude the fields stats
, value
, and comments
when returning documents, you would use None
for the query
object because you are not finding all objects. You also would use the following fields
object:
fields = {'stats' : false, 'value' : false, 'comments' : False);
cursor = myColl.find(None, fields)
Including just a few fields is often easier. For example, if you want to include only the word
and size
fields of documents in which the first
field equals t
, you would use
query = {'first' : 't'}
fields = {'word' : true, 'size' : True}
cursor = myColl.find(query, fields)
A common method of reducing the number of documents returned is to use paging. Paging involves specifying both a number of documents to skip in the matching set and a limit on the documents returned. The skip value is incremented each time by the amount returned the previous time.
To implement paging on a set of documents, you need to implement the limit()
and skip()
methods on the Cursor
object. The skip()
method enables you to specify a number of documents to skip before returning documents.
By incrementing the value used in the skip()
method by the size used in limit()
each time you get another set of documents, you can effectively page through the dataset.
For example, the following statements find documents 11–20:
cursor = collection.find()
cursor.limit(10)
cursor.skip(10)
Always include a sort
option when paging data, to ensure that the order of documents is the same.
A useful query against a MongoDB collection is to get a list of the distinct values for a single field in a set of documents. Distinct means that even though thousands of documents exist, you want to know only the unique values.
The distinct()
method on Collection
and Cursor
objects enables you to find a list of distinct values for a specific field. The syntax for the distinct()
method follows:
distinct(key)
The key
parameter is the string value of the field name you want to get values for. You can specify subdocuments using dot syntax, such as stats.count
. If you want to get distinct values based on a subset of documents, you need to first generate a Cursor
object with a query
parameter and then call distinct()
on that Cursor
object.
For example, to find the distinct last names of users over 65 in a collection that has documents with first
, last
, and age
fields, you would use the following operation:
query = {'age' : {'$gt' : 65}}
cursor = myCollection.find(query)
lastNames = cursor.distinct('last')
The distinct()
method returns an array with the distinct values for the field specified. For example:
["Smith", "Jones", ...]
When performing operations on large datasets in Python, grouping the results based on the distinct values of one or more fields in a document is often useful. This can be done in code after retrieving the documents, but it is much more efficient to have the MongoDB server do it for you as part of a single request that is already iterating though the documents.
In Python, to group the results of a query, you can use the group()
method on the Collection
object. The group request collects all the documents that match a query
, adds a group object to an array based on distinct values of a set of keys
, performs operations on the group objects, and returns the array of group objects.
The syntax for the group()
methods follows:
group({key, cond , initial, reduce, [finalize]})
The key
, cond
, and initial
parameters are a Dictionary
object that define the fields to use, query, and limit documents and initial value settings. The reduce
and finalize
methods are String
objects that contain a string form of a JavaScript function that is run on the server to reduce and finalize the results. See Hour 9, “Utilizing the Power of Grouping, Aggregation, and Map Reduce,” for more information on these parameters.
For illustration, the following code implements a basic grouping by generating the key
, cond
, and initial
objects and passing in a reduce
function as a string:
key = {'first' : True }
cond = {'first' : 'a', 'size': 5}
initial = {'count' : 0}
reduce = "function (obj, prev) { prev.count++; }"
results = collection.group(key, cond, initial, reduce)
The result from the group
method is a List
that contains the groupings. To illustrate the results, the following code displays the items in the group one at a time:
for result in results:
print (result)
Another valuable tool when working with MongoDB in Python applications is the aggregation framework. The Collection
object provides the aggregate()
method to perform aggregation operations on data. The syntax for the aggregate()
method follows:
aggregate(operator, [operator, ...])
The operator
parameter is a list of one or more Operator
objects that provide the pipeline for aggregating the results. Each Operator
object is a Dictionary
built with the operators. Hour 9 described the aggregation operators, so you should already be familiar with them.
As an example, the following code creates $group
and $limit
operators. The $group
operator groups by _id
of the word
field and adds an average
field using the $avg
that averages a field named size
. Notice that the field names must be prefixed with $
in aggregation operations:
group = {'$group' :
{'_id' : '$word',
'average' : {'$avg' : '$size'}}}
limit = {'$limit' : 10}
result = collection.aggregate([group, limit])
The result from the aggregate()
method is a Dictionary
object that contains the aggregation results. This Dictionary
contains a key named result
that is a list of the aggregated results. To illustrate, the following code accesses and displays the items in the aggregated results one at a time:
for result in results['result']:
print (result)
In this hour, you learned how to use additional methods on the Collection
and Cursor
objects. You learned that the limit()
method can reduce the documents the cursor returns and that limit()
and skip()
enable you to page through a large dataset. Using a fields
parameter on the find()
method enables you to reduce the number of fields returned from the database.
This hour also covered applying the distinct()
, group()
, and aggregate()
methods on the Collection
object to perform data gathering operations from a Python application. These operations enable you to process data on the server before returning it to the Python application, reducing the amount of data sent and the work required in the application.
Q. Is there a way to programmatically execute MongoDB commands from a Python application?
A. Yes. The Python MongoDB driver provides the command()
method on the Database
object that accepts a command that will be executed on the MongoDB server.
Q. Is there a way to convert Python Dictionary objects to and from BSON objects?
A. Yes. The Python MongoDB driver provides the bson.BSON
class that has decode(dict)
and encode(BSON)
methods to encode and decode BSON objects to and from dictionaries.
The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try answering the questions before looking at the answers.
1. How do you get the 21–30 documents represented by a Cursor
object in Python?
2. How do you find the different values for a specific field on documents in a collection in a Python application?
3. How do you return the first 10 documents from a collection in Python?
4. How do you prevent a specific field from being returned from a database query in Python?
1. Call limit(10)
and skip(20)
on the Cursor
object.
2. Use the distinct()
method on the Collection
object.
3. Use the limit(10) method on the Cursor
object.
4. Set the field to False
in the fields
object passed to the find()
method.
1. Write a new Python application that finds words in the example dataset that start with n
, sort them by length descending, and then display the top five.
2. Extend the PythonAggregate.py
file to include a function that performs an aggregate that matches the words with a length of 4
, limits it to only five documents, and finally projects the word as the _id
field and displays the stats. The matching MongoDB shell aggregation would look similar to the following:
{$match: {size:4}},
{$limit: 5},
{$project: {_id:"$word", stats:1}}
18.118.121.54