Python is a powerful programming language used for a diverse range of applications, including web development to desktop apps. MongoDB, on the other hand, is a document-oriented database management system that uses BSON format for the data storage. In this tutorial, we will look at how to query MongoDB with Python.
Prerequisites
- Python 3.x installed on your computer
- Pymongo Python package installed using pip
- MongoDB installed and running on your computer.
MongoDB Queries
In MongoDB, data is organized in collections and documents, which are similar to tables and rows in a relational database. MongoDB has a powerful query language that allows you to retrieve data from collections based on certain criteria. You can use various query operators to perform complex query operations such as sorting, matching, selecting, and filtering data.
Connecting to MongoDB with Python
Before we start querying MongoDB with Python, we need to establish a connection with MongoDB using Python. We can use the PyMongo Python package to connect to the MongoDB instance running on our machine.
#importing the PyMongo package import pymongo #creating a connection with MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/")
In the above code, we first import the pymongo package, and then we create a connection to the MongoDB instance running on our machine. We use the URI of the MongoDB server to establish a connection.
Querying Documents from a Collection
Now that we have established a connection with MongoDB using PyMongo, let’s look at how to query documents from a collection.
First, we need to select the collection from which we want to retrieve documents. We can do this using the collection() method of the pymongo package:
#connecting to a database db = client['testdb'] #selecting a collection collection = db['testcollection']
Now let’s look at some of the commonly used methods for querying documents from a MongoDB collection:
Find All Documents
The find() method is used to retrieve all documents from a collection:
#retrieving all documents from collection documents = collection.find() #looping through all the documents for document in documents: print(document)
Output:
{'_id': ObjectId('61b422f0b1c53740461521cb'), 'name': 'John Doe', 'age': 30, 'country': 'USA'} {'_id': ObjectId('61b422f0b1c53740461521cc'), 'name': 'Jane Doe', 'age': 35, 'country': 'Canada'}
In the above code, we first retrieve all documents from the collection using the find() method. We then loop through each document and print it.
Find Documents Matching a Criteria
The find() method can also be used to retrieve only those documents that match a specific criteria. We can pass a query document as a parameter to the find() method to specify the criteria:
#retrieving documents matching a specific criteria documents = collection.find({"country": "USA"}) #looping through all the documents for document in documents: print(document)
Output:
{'_id': ObjectId('61b422f0b1c53740461521cb'), 'name': 'John Doe', 'age': 30, 'country': 'USA'}
In the above code, we retrieve all documents from the collection where the “country” field is set to “USA”. We then loop through each document and print it.
Find One Document
The find_one() method is used to retrieve one document from a collection that matches a specific criteria:
#retrieving one document from collection matching a specific criteria document = collection.find_one({"name": "John Doe"}) #printing the document print(document)
Output:
{'_id': ObjectId('61b422f0b1c53740461521cb'), 'name': 'John Doe', 'age': 30, 'country': 'USA'}
In the above code, we retrieve one document from the collection where the “name” field is set to “John Doe”. We then print the document.
Limiting the Number of Documents Returned
We can limit the number of documents returned by the find() method by passing the limit parameter:
#retrieving only 1 document from collection documents = collection.find().limit(1) #printing the document for document in documents: print(document)
Output:
{'_id': ObjectId('61b422f0b1c53740461521cb'), 'name': 'John Doe', 'age': 30, 'country': 'USA'}
In the above code, we retrieve only one document from the collection using the limit() method. We then loop through each document and print it.
Sorting Documents
We can sort the retrieved documents based on any field by passing the field name as a parameter to the sort() method:
#sorting documents by age in descending order documents = collection.find().sort("age", -1) #printing the documents for document in documents: print(document)
Output:
{'_id': ObjectId('61b422f0b1c53740461521cc'), 'name': 'Jane Doe', 'age': 35, 'country': 'Canada'} {'_id': ObjectId('61b422f0b1c53740461521cb'), 'name': 'John Doe', 'age': 30, 'country': 'USA'}
In the above code, we first retrieve all documents from the collection, and then we sort them based on the “age” field in descending order using the sort() method. We then loop through each document and print it.
Selecting Fields to Return
We can select specific fields to return using the projection parameter in the find() method:
#selecting only name and age fields to retrieve documents = collection.find({}, {"name": 1, "age": 1, "_id": 0}) #printing the documents for document in documents: print(document)
Output:
{'name': 'John Doe', 'age': 30} {'name': 'Jane Doe', 'age': 35}
In the above code, we retrieve all documents from the collection and select only the “name” and “age” fields to return using the projection parameter. We exclude the “_id” field using the value 0. We then loop through each document and print it.
Conclusion
In this tutorial, we looked at how to query MongoDB with Python. We started by establishing a connection to MongoDB using PyMongo, and then we looked at some commonly used query methods for retrieving documents from a MongoDB collection.