Python MongoDB
In this page:
from pymongo import MongoClient
client = MongoClient("connection_string")
collection = client["database"]["collection"]
collection.insert_one({"key": value})
MongoDB से Connect करना
pymongo library Python से MongoDB से बात करने के लिए standard driver है — आप अपने connection string की ओर इशारा करता एक MongoClient बनाते हैं, फिर उसमें से एक database और collection चुनते हैं, कुछ-कुछ उसी तरह जैसे इस site पर कहीं और cover किए गए relational world में mysql.connector कोई connection खोलता है।
उदाहरण: Connecting to MongoDB
# pip install pymongo
from unittest.mock import MagicMock
client = MagicMock()
db = client["mydb"]
collection = db["users"]
print(type(collection))
Documents Insert करना
insert_one() किसी collection में एक single document (एक Python dict) जोड़ता है, और insert_many() एक साथ कई जोड़ने के लिए dicts की एक list लेता है — किसी SQL table के विपरीत, एक ही collection में MongoDB documents के fields identical होने ज़रूरी नहीं, क्योंकि यह एक schemaless document store है।
उदाहरण: Inserting Documents
from unittest.mock import MagicMock
collection = MagicMock() # stands in for a real MongoDB collection
collection.insert_one({"name": "Alex"}) # adds a single document
collection.insert_many([{"name": "Sam"}, {"name": "Jo"}]) # adds several documents at once
print("Documents inserted")
Documents को Query करना
find_one() पहला matching document या None लौटाता है, जबकि find() एक cursor लौटाता है जिस पर हर match पाने के लिए iterate किया जाता है — find() को filter के रूप में एक खाली dict {} पास करना collection के हर document से match करता है, कुछ-कुछ बिना WHERE clause वाले SQL query जैसा।
उदाहरण: Querying Documents
from unittest.mock import MagicMock
collection = MagicMock()
collection.find_one.return_value = {"name": "Alex"}
collection.find.return_value = [{"name": "Alex"}, {"name": "Sam"}]
print(collection.find_one({"name": "Alex"})) # returns the first match, or None
print(list(collection.find({}))) # empty filter matches every document
Documents को Update करना
update_one() पहले matching document को बदलता है, और इसके लिए $set जैसा update operator चाहिए होता है यह बताने के लिए कि कौन से fields बदलने हैं — $set भूल जाना और replacement fields सीधे पास कर देना पूरे document को replace कर देगा, और जिन fields को आपने शामिल नहीं किया वो मिट जाएँगे।
उदाहरण: Updating Documents
from unittest.mock import MagicMock
collection = MagicMock()
collection.update_one({"name": "Alex"}, {"$set": {"age": 31}}) # $set changes only this field
print("Document updated with $set")
Documents को Delete करना
delete_one() पहला matching document हटाता है और delete_many() हर match हटाता है — दोनों एक result object लौटाते हैं जिसका deleted_count बताता है कि असल में कितने documents हटाए गए, जो जाँचने लायक है क्योंकि कुछ भी match न करने वाला filter error raise करने की बजाय चुपचाप शून्य documents delete करता है।
उदाहरण: Deleting Documents
from unittest.mock import MagicMock
collection = MagicMock()
collection.delete_one.return_value.deleted_count = 1
result = collection.delete_one({"name": "Alex"})
print(result.deleted_count) # how many documents were actually removed
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB