Video Transcript (English)
Hello!
In this video, you’ll learn how to query a MongoDB collection by using the PyMongo driver.
There are two methods that we can use to query in MongoDB: find_one() is used to return a single document that matches a query. While find() is used to return all documents that match a query.
Let’s start with an example of the most basic type of query that can be performed in MongoDB, which is find_one().
find_one() returns a single document that matches a query. Or it returns None if there are no matches.
find_one() is useful in cases where we know there is only one matching document, or if we’re only interested in the first match.
In this example, we’ll work with a database named bank that contains an accounts collection.
Let’s say we want to retrieve the document from the ‘accounts’ collection that matches this _id value.
It’s appropriate to use find_one() for this query because there can be only one document in a collection that matches any given `_id` value.
We’ve already opened a Python file and connected to our Atlas cluster with MongoClient.
We’ve also created references to the database and collection that we want to operate on.
We have a reference to the `bank` database and a reference to the `accounts` collection.
We write a filter in the form of a dictionary that specifies the constraint for our query. In this case, we’re filtering by a specific ObjectId value.
We assign this filter to the variable named `document_to_find`.
Now we’re ready to write the expression that retrieves the document that matches our filter.
We call the find_one() method on the accounts_collection object.
And we pass in our filter, document_to_find, as an argument to find_one.
To print out the document that’s returned, we assign the result to a variable and pretty print the result.
Finally, we close the client.
One important note before we run our code:
To work with ObjectIds in PyMongo, we need to import the ObjectId class from PyMongo’s `bson` package.
We add this statement to our imports.
More details about working with MongoDB ObjectIds can be found in the PyMongo documentation.
Ok, now we’re ready. Let’s run our code.
The document that matches the _id value that we specified is printed out.
Now let’s go through an example of querying for multiple documents.
We want to find all accounts that have a balance greater than $4700.
We’ve created a new Python file, connected to our Atlas cluster with MongoClient, and created references to the database and collection.
We need a filter to select documents that meet our constraint. So, we write a query document that selects documents where the “balance” field’s value is greater than 4700. Note that we’re using the query operator $gt to select documents where the balance field’s value is greater than our specified value.
We assign this query filter to a variable named ‘documents_to_find’.
Now we’re ready to write the expression that carries out the query.
We call the find() method on the accounts_collection object.
And we pass in our filter, documents_to_find.
Note that if we run a find() query without a filter, all documents in the collection will be included in the result set.
find() returns a cursor instance, which allows us to iterate over all matching documents.
We assign the result of the find operation to a variable named 'cursor'.
Let’s say that we want to print out the total number of documents found by this query, as well as the documents themselves.
To do this, we first declare a counter variable to keep track of the number of documents matched by the query.
Then we use a for-loop to iterate over the documents in the cursor. In each iteration, we increment the counter and print out the corresponding document.
Finally, we print out the total number of documents that were found and close the client.
Now let’s run the code. The documents returned by the query are printed out, as well the total number of documents that were found, 4.
Great job!
In this video, you learned how to query MongoDB collections by using PyMongo.
We used find_one() to return a single document that matches a query. And we used find() to return all documents that match a query.
We also used query operators, such as $gt, to help refine our search for documents within a collection.