Python MongoDB Tutorial – How to Create a Collection with PyMongo
Last updated 5 months, 1 week ago | 458 views 75 5

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. In this tutorial, you’ll learn how to create a collection in MongoDB using Python and the PyMongo
library, including best practices and code examples.
Table of Contents
-
What Is a Collection in MongoDB?
-
Why Collections Matter
-
Prerequisites
-
Installing PyMongo
-
Connecting to MongoDB
-
Creating a Database
-
Creating a Collection
-
Inserting a Document (Triggers Creation)
-
Verifying Collection Creation
-
Full Working Example
-
Tips and Common Pitfalls
1. What Is a Collection in MongoDB?
A collection in MongoDB is similar to a table in a relational database. It is a group of MongoDB documents, which are stored in a database.
Each document in a collection can have a different structure, allowing for flexible schema design.
❓ 2. Why Collections Matter
-
Collections organize related data (like tables in SQL)
-
You query, update, and manage data within collections
-
Collections are automatically created when data is inserted
⚙️ 3. Prerequisites
-
Python installed (3.x recommended)
-
MongoDB server running locally or via Atlas
-
pymongo
installed
4. Installing PyMongo
Install the official MongoDB Python driver:
pip install pymongo
5. Connecting to MongoDB
Use MongoClient
to connect to a MongoDB server.
Local MongoDB Connection
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
Remote MongoDB (MongoDB Atlas)
client = pymongo.MongoClient("mongodb+srv://username:[email protected]/?retryWrites=true&w=majority")
6. Creating a Database
Specify or create a database:
db = client["mydatabase"]
If the database doesn't exist, MongoDB will create it when you insert data into a collection.
7. Creating a Collection
You can create a collection explicitly using:
collection = db.create_collection("customers")
⚠️ This will throw an error if the collection already exists. To avoid this, check first:
if "customers" not in db.list_collection_names():
db.create_collection("customers")
Alternatively, you can implicitly create a collection just by accessing it and inserting data:
collection = db["customers"] # This doesn't create the collection yet
✍️ 8. Inserting a Document (Triggers Creation)
If using implicit creation, inserting a document will cause the collection to be created:
collection.insert_one({"name": "Alice", "email": "[email protected]"})
✅ 9. Verifying Collection Creation
You can list all collections in the database to confirm:
print(db.list_collection_names())
If "customers"
appears, your collection was successfully created.
10. Full Working Example
import pymongo
# Connect to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Create a database
db = client["mydatabase"]
# Check if collection exists
if "customers" not in db.list_collection_names():
db.create_collection("customers")
print("Collection 'customers' created.")
else:
print("Collection already exists.")
# Insert a document (optional)
customer = {"name": "Alice", "email": "[email protected]"}
db["customers"].insert_one(customer)
# Verify
print("Collections:", db.list_collection_names())
Output:
Collection 'customers' created.
Collections: ['customers']
11. Tips and Common Pitfalls
Tip or Pitfall | Recommendation |
---|---|
Collection not showing | MongoDB won't create it until data is inserted |
Collection already exists error | Use list_collection_names() to check before creating |
Forgetting to insert | Implicit creation needs at least one document |
NoSQL schema | MongoDB collections don’t enforce strict schemas – be cautious about inconsistent data |
Conclusion
Creating a collection in MongoDB using Python is simple with the help of the pymongo
library. Whether you choose to create it explicitly or rely on implicit creation via document insertion, MongoDB’s flexibility makes it easy to structure your data your way.
Collections are the foundation of any MongoDB application—get them right, and you’ll have a solid structure for managing your data efficiently.