Python > Working with Data > Databases > NoSQL Databases - Introduction to MongoDB with `pymongo`
Connecting to MongoDB and Inserting a Document
This snippet demonstrates how to connect to a MongoDB database using `pymongo` and insert a single document into a collection. It includes error handling for connection issues.
Code:
The code first imports the `pymongo` library. It then defines the connection string (`mongo_uri`), database name (`db_name`), and collection name (`collection_name`). The `try...except...finally` block is used for robust error handling. Inside the `try` block, a connection to MongoDB is established using `pymongo.MongoClient()`. Then, it accesses the specified database and collection. A sample document (a Python dictionary) is created and inserted into the collection using `collection.insert_one()`. The `inserted_id` attribute of the result object provides the unique ID of the newly inserted document. The `except` block catches `pymongo.errors.ConnectionFailure` exceptions, printing an error message if the connection fails. The `finally` block ensures that the connection is closed, even if exceptions occur. It checks if the `client` variable exists to prevent errors if the connection failed before it could be assigned.
import pymongo
# Replace with your MongoDB connection string
mongo_uri = "mongodb://localhost:27017/"
# Database and collection names
db_name = "mydatabase"
collection_name = "mycollection"
try:
# Establish a connection to MongoDB
client = pymongo.MongoClient(mongo_uri)
# Access the database
db = client[db_name]
# Access the collection
collection = db[collection_name]
# Sample document to insert
document = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
# Insert the document
result = collection.insert_one(document)
# Print the inserted document's ID
print(f"Inserted document with ID: {result.inserted_id}")
except pymongo.errors.ConnectionFailure as e:
print(f"Could not connect to MongoDB: {e}")
finally:
# Close the connection (optional but recommended)
if 'client' in locals(): # Check if the client was successfully created
client.close()
Concepts Behind the Snippet
This snippet illustrates the fundamental concepts of connecting to a MongoDB server, accessing a specific database and collection, and inserting a single document. MongoDB stores data in collections of JSON-like documents. The `pymongo` driver allows you to interact with MongoDB using Python objects, translating them into the BSON format used by MongoDB.
Real-Life Use Case
This operation is useful in scenarios where you need to store structured data, such as user profiles, product information, or sensor readings. Imagine a web application where user information is stored in a MongoDB database. When a new user registers, their details (name, email, password, etc.) can be inserted into the 'users' collection using this code as a starting point.
Best Practices
Interview Tip
Be prepared to discuss the differences between relational databases (SQL) and NoSQL databases like MongoDB. Explain the advantages of MongoDB's flexible schema and scalability, as well as its use cases. Also, be able to articulate how to connect to a MongoDB instance and perform basic CRUD operations using `pymongo`.
When to Use Them
Use MongoDB when you need a flexible schema, horizontal scalability, and high performance for unstructured or semi-structured data. It is well-suited for applications with rapidly changing data models or high read/write loads.
Alternatives
Alternative NoSQL databases include Cassandra, Couchbase, and Redis. SQL databases like PostgreSQL, MySQL, and SQL Server are alternatives for structured data with well-defined schemas.
Pros
Cons
FAQ
-
What is the `inserted_id`?
The `inserted_id` is a unique identifier automatically generated by MongoDB for each document inserted into a collection. It is a BSON ObjectId. -
How do I handle authentication with MongoDB?
You can include authentication credentials in the connection string, e.g., `mongodb://username:password@localhost:27017/`. For more secure deployments, use authentication mechanisms like SCRAM-SHA-256 and store credentials securely. -
What if the database or collection doesn't exist?
MongoDB will automatically create the database and collection when you first access them and insert data. You don't need to explicitly create them beforehand.