← Back to Python Course | Chapter 13: Data Science & Web | Lesson 14 of 14

Python MongoDB

MongoDB एक database है जो जानकारी को flexible labeled documents के रूप में store करता है, जैसे अलग-अलग notes वाले index cards। Python उन cards को add, find, change, और delete कर सकता है।
Syntax
python
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

python
# 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

python
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

python
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

python
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

python
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.