Python MongoDB Query
In MongoDB, querying allows you to find documents in a collection that match specific criteria. With PyMongo, the Python driver for MongoDB, you can perform various types of queries to retrieve data from MongoDB collections.
Types of Queries in MongoDB
- Basic Queries: Retrieve documents based on exact match criteria.
- Comparison Queries: Use comparison operators (like
$gt,$lt, etc.) to filter data. - Logical Queries: Combine multiple conditions using logical operators (
$and,$or, etc.). - Projection: Specify which fields to include or exclude from the result set.
- Sorting: Order the query results based on one or more fields.
- Limiting: Restrict the number of documents returned.
Querying Documents in MongoDB Using Python (PyMongo)
Step 1: Install PyMongo (if not already installed)
pip install pymongo
Step 2: Connect to MongoDB and Select a Database and Collection
import pymongo
# Connect to MongoDB server
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Access the database (it will be created if it doesn't exist)
mydb = client["mydatabase"]
# Access the collection (it will be created if it doesn't exist)
mycol = mydb["customers"]
1. Basic Query (Exact Match)
To retrieve documents that match specific criteria, pass a query filter (as a Python dictionary) to the find() method.
# Query to find documents where 'name' is 'John'
query = { "name": "John" }
# Retrieve documents that match the query
results = mycol.find(query)
# Print the matching documents
for doc in results:
print(doc)
2. Comparison Queries
MongoDB supports various comparison operators like $gt (greater than), $lt (less than), $gte (greater than or equal), $lte (less than or equal), $ne (not equal), and $in (matches any value in an array).
# Query to find documents where 'age' is greater than 25
query = { "age": { "$gt": 25 } }
results = mycol.find(query)
for doc in results:
print(doc)
Common comparison operators:
$gt: Greater than$lt: Less than$gte: Greater than or equal$lte: Less than or equal$ne: Not equal$in: Matches any value in a list$nin: Does not match any value in a list
3. Logical Queries
You can use logical operators like $or, $and, $not, and $nor to create more complex queries.
# Query to find documents where 'name' is 'John' OR 'age' is greater than 30
query = { "$or": [ { "name": "John" }, { "age": { "$gt": 30 } } ] }
results = mycol.find(query)
for doc in results:
print(doc)
Other logical operators:
$and: Match all conditions$or: Match any condition$not: Negate a condition$nor: Match none of the conditions
4. Field Projection
Projection specifies which fields should be included or excluded from the result. You can include (1) or exclude (0) fields in the projection.
# Query to find documents where 'name' is 'John', and only return 'name' and 'address'
query = { "name": "John" }
projection = { "name": 1, "address": 1, "_id": 0 } # Exclude '_id'
results = mycol.find(query, projection)
for doc in results:
print(doc)
5. Sorting Query Results
You can use the sort() method to order the query results based on a field in either ascending (1) or descending (-1) order.
# Query to find all documents and sort by 'name' in ascending order
results = mycol.find().sort("name", 1)
for doc in results:
print(doc)
6. Limiting Query Results
You can use the limit() method to restrict the number of documents returned.
# Query to find the first 5 documents where 'name' is 'John'
query = { "name": "John" }
results = mycol.find(query).limit(5)
for doc in results:
print(doc)
7. Using Regular Expressions in Queries
MongoDB supports regular expressions for pattern matching in string fields.
# Query to find documents where 'name' starts with 'J'
query = { "name": { "$regex": "^J" } }
results = mycol.find(query)
for doc in results:
print(doc)
8. Finding Documents with Embedded Fields
You can query documents based on values inside embedded fields (nested dictionaries).
# Example document: { "name": "John", "address": { "city": "New York", "zip": "10001" } }
# Query to find documents where 'city' in the 'address' field is 'New York'
query = { "address.city": "New York" }
results = mycol.find(query)
for doc in results:
print(doc)
Example: Full Query with Multiple Operations
Here’s an example of combining queries with projections, sorting, and limiting:
import pymongo
# Connect to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["mydatabase"]
mycol = mydb["customers"]
# Insert some sample documents (if not already inserted)
mycol.insert_many([
{ "name": "John", "age": 25, "address": "123 Elm Street" },
{ "name": "Amy", "age": 32, "address": "456 Oak Street" },
{ "name": "Mark", "age": 28, "address": "789 Maple Avenue" },
{ "name": "Sara", "age": 24, "address": "101 Pine Road" }
])
# Query: Find all 'Johns', return only 'name' and 'age', sort by 'age', and limit to 2 results
query = { "name": "John" }
projection = { "name": 1, "age": 1, "_id": 0 }
results = mycol.find(query, projection).sort("age", 1).limit(2)
# Print the results
for doc in results:
print(doc)
Summary:
- Basic Queries: Use
find()with a filter to query documents based on exact matches. - Comparison Queries: Use operators like
$gt,$lt, and$into filter results. - Logical Queries: Use
$or,$and, and$notto combine multiple conditions. - Projection: Specify which fields to include or exclude from the result set.
- Sorting: Use
sort()to order the query results. - Limiting: Use
limit()to restrict the number of documents returned.