liveBook · Manning

MongoDB

As explained in The Quick Python Book, Fourth Edition

MongoDB is a document-oriented NoSQL database system designed for distributed storage and querying of structured data.

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

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Demonstrates basic PyMongo CRUD and setup, but stops short of MongoDB’s broader capabilities and design details.
Examples
Demonstrates a Jane document, generated ObjectId, field update, full replacement, deletion, and collection dropping.
What it assumes
Requires Python, PyMongo, and access to a MongoDB server through Atlas, Docker, or a local installation.
What you can do after
Operate/configure MongoDB through PyMongo, including connecting, creating documents, querying, modifying, deleting, and dropping collections.
How it connects
Connects MongoDB with NoSQL databases, BSON, JSON, Python, PyMongo, Docker, MongoDB Atlas, and relational databases.

Also explained in

Compare how 7 books explain MongoDB →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.