Python MongoDB Tutorial
×

Python MongoDB Tutorial

104

Python MongoDB Tutorial

MongoDB is a widely-used NoSQL database that stores data in flexible, JSON-like documents. Unlike traditional relational databases, MongoDB doesn't rely on tables and rows but uses collections and documents, making it ideal for handling unstructured or semi-structured data. This tutorial will guide you through integrating MongoDB with Python using the PyMongo library.

Installing PyMongo

To interact with MongoDB through Python, you'll need to install the pymongo library. Use pip to install it:

pip install pymongo

Connecting to MongoDB

After installing PyMongo, you can establish a connection to your MongoDB server:

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]

This code connects to the MongoDB server running on localhost and accesses (or creates) a database named mydatabase.

Creating a Collection

In MongoDB, a collection is similar to a table in relational databases. You can create a collection as follows:

collection = db["customers"]

This creates a collection named customers within the mydatabase database.

Inserting Documents

Documents are records in MongoDB, represented as dictionaries in Python. You can insert a single document:

customer = {"name": "John", "address": "Highway 37"}
collection.insert_one(customer)

Or insert multiple documents at once:

customers = [
    {"name": "Amy", "address": "Apple st 652"},
    {"name": "Hannah", "address": "Mountain 21"},
    {"name": "Michael", "address": "Valley 345"}
]
collection.insert_many(customers)

Querying the Database

To retrieve documents from a collection, use the find() method:

for customer in collection.find():
    print(customer)

You can also filter the results:

query = {"address": "Highway 37"}
customer = collection.find_one(query)
print(customer)

Updating Documents

To update existing documents, use the update_one() or update_many() methods:

query = {"address": "Valley 345"}
new_values = {"$set": {"address": "Canyon 123"}}
collection.update_one(query, new_values)

Deleting Documents

To delete documents, use the delete_one() or delete_many() methods:

query = {"address": "Mountain 21"}
collection.delete_one(query)

Sorting Results

You can sort the results of a query using the sort() method:

for customer in collection.find().sort("name"):
    print(customer)

Conclusion

Integrating MongoDB with Python using PyMongo allows for efficient handling of NoSQL databases. With the ability to perform CRUD operations and more, you can build robust applications that manage data effectively.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat