Creating Collections in MongoDB

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. A collection in MongoDB is similar to a table in a relational database. It is a group of MongoDB documents, and is the equivalent of an RDBMS table. Collections do not enforce a schema, which means documents within a single collection can have different structures.

Setting Up MongoDB and Python Environment

Before working with MongoDB in Python, you need to make sure you have MongoDB installed on your system and the Python MongoDB driver (pymongo) installed. You can install pymongo using pip:

				
					pip install pymongo
				
			

Next, ensure MongoDB server is running on your system. You can start MongoDB server using:

				
					mongod
				
			

Connecting to MongoDB from Python

Now, let’s write Python code to connect to MongoDB:

				
					import pymongo

# Establishing a connection to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")

# Accessing a database
db = client["mydatabase"]
				
			

Explaination:

  • pymongo.MongoClient() creates a connection to the MongoDB server running on localhost.
  • client["mydatabase"] accesses a database named “mydatabase”.

Creating Collections in MongoDB

To create a collection in MongoDB, you can simply start using it. MongoDB will create the collection if it does not exist when you first store data into it. Let’s create a collection named “customers”:

				
					# Creating a collection
customers = db["customers"]
				
			

Explanation:

  • Now, the collection “customers” is created in the “mydatabase” database.

Inserting Documents into a Collection

Once the collection is created, you can insert documents into it. Documents in MongoDB are represented as dictionaries in Python. Let’s insert a document into the “customers” collection:

				
					# Inserting a document
customer_data = {"name": "John", "email": "john@example.com", "age": 30}
inserted_id = customers.insert_one(customer_data).inserted_id
print("Document inserted with id:", inserted_id)
				
			

Explanation:

  • In the above code, we inserted a document with fields “name”, “email”, and “age” into the “customers” collection. The insert_one() method returns the id of the inserted document.

Querying Documents in a Collection

You can retrieve documents from a collection using various query methods. Let’s query all documents in the “customers” collection:

				
					# Querying documents
for customer in customers.find():
    print(customer)
				
			

Explanation:

  • This code retrieves all documents from the “customers” collection and prints them.

We learned about creating collections in MongoDB using Python. We covered setting up the environment, connecting to MongoDB, creating collections, inserting documents, and querying documents. MongoDB's flexibility and ease of use make it a powerful choice for managing data in Python applications.
By following these steps, you can effectively work with MongoDB collections in your Python projects. Experiment with different operations and explore the capabilities of MongoDB to better understand its usage in real-world applications. Happy Coding!❤️

Table of Contents