MongoDB is a popular NoSQL database that stores data in JSON-like documents. This tutorial will guide you through the basics of interacting with MongoDB using Python, covering operations such as inserting, finding, updating, and deleting documents.
Before we begin, ensure you have the following installed:
pip install pymongo
To interact with MongoDB, you need to establish a connection. Here’s how you can do it:
from pymongo import MongoClient
# Connect to the MongoDB server running on localhost at port 27017
client = MongoClient('mongodb://localhost:27017/')
# Access the database named 'mydatabase'
db = client['mydatabase']
# Access the collection named 'mycollection'
collection = db['mycollection']
You can insert documents into a collection using the insert_one or insert_many methods.
# Define a document to be inserted
document = {
"name": "Alice",
"age": 30,
"city": "New York"
}
# Insert the document into the collection
result = collection.insert_one(document)
# Print the inserted document's ID
print(f"Inserted document with ID: {result.inserted_id}")
# Define multiple documents to be inserted
documents = [
{"name": "Bob", "age": 25, "city": "Los Angeles"},
{"name": "Charlie", "age": 35, "city": "Chicago"}
]
# Insert the documents into the collection
result = collection.insert_many(documents)
# Print the inserted document IDs
print(f"Inserted documents with IDs: {result.inserted_ids}")
You can retrieve documents from a collection using various query methods.
# Find a single document where 'name' is 'Alice'
document = collection.find_one({"name": "Alice"})
# Print the found document
print(document)
# Find all documents where 'age' is greater than 25
documents = collection.find({"age": {"$gt": 25}})
# Print each found document
for doc in documents:
print(doc)
You can update documents using the update_one or update_many methods.
# Update the 'age' of the document where 'name' is 'Alice'
result = collection.update_one({"name": "Alice"}, {"$set": {"age": 31}})
# Print the number of documents modified
print(f"Modified count: {result.modified_count}")
# Update the 'city' of all documents where 'age' is greater than 25
result = collection.update_many({"age": {"$gt": 25}}, {"$set": {"city": "San Francisco"}})
# Print the number of documents modified
print(f"Modified count: {result.modified_count}")
You can delete documents using the delete_one or delete_many methods.
# Delete the document where 'name' is 'Bob'
result = collection.delete_one({"name": "Bob"})
# Print the number of documents deleted
print(f"Deleted count: {result.deleted_count}")
# Delete all documents where 'age' is greater than 30
result = collection.delete_many({"age": {"$gt": 30}})
# Print the number of documents deleted
print(f"Deleted count: {result.deleted_count}")
This tutorial has covered the basics of interacting with MongoDB using Python, including inserting, finding, updating, and deleting documents. By following these steps and best practices, you can effectively manage data in MongoDB using Python.
Feel free to explore more advanced features of PyMongo and MongoDB to enhance your applications.