MongoDB is a document-oriented NoSQL database that stores BSON documents as key-value pairs resembling JSON objects or Python dictionaries. It avoids fixed row-and-column structure and is designed to scale across many nodes and clusters. section 23.7 section 23.5
Use PyMongo to work with MongoDB at the database, collection, and document levels:
- Select a database with client.my_data and a collection with db.docs; MongoDB can defer creating them until data is inserted. section 23.7
- Insert a Python dictionary with collection.insert(), then retrieve documents with find_one(). MongoDB adds a unique ObjectId in the _id field. section 23.7
- Match a document by _id or another specification, modify fields with update_one() and $set, replace it with replace_one(), or remove it with delete_one(). section 23.7
- Drop a collection when its documents are no longer needed. section 23.7
A small document workflow
from pymongo import MongoClient import datetime client = MongoClient("<connection string>") db = client.my_data collection = db.docs doc = {"name": "Jane", "age": 34, "interests": ["Python", "databases", "statistics"], "date_added": datetime.datetime.now()} collection.insert(doc) collection.update_one({"_id": doc["_id"]}, {"$set": {"name": "Ann"}}) collection.find_one({"_id": doc["_id"]}) section 23.7
! pip install pymongo
Install the Python client before connecting to a MongoDB server. section 23.7
A free MongoDB Atlas cluster is a convenient experimentation target; Docker can also run MongoDB locally. MongoDB suits scale, distribution, high insert rates, and complex or unstable schemas, but requirements should determine the database choice. section 23.7






