MongoDB from Python: queries without SQL, and typed documents through an asynchronous mapper.
Before this guide: APIs and JSON.
The notebooks
Open in Colab runs a notebook in your browser, and Read shows it on GitHub without running it. Every notebook runs on its own, and the answers to its exercises are in a separate notebook, linked as Solutions.
| 1 | Why DocumentsOne object instead of five tables, and a MongoDB running inside this notebook | Open in ColabReadSolutions |
| 2 | Collections and Documentsinsert_one, insert_many, the _id you did not choose, and the collection a typo invents | Open in ColabReadSolutions |
| 3 | BSON TypesThe set you cannot store, the datetime that comes back naive, and the ObjectId json.dumps refuses | Open in ColabReadSolutions |
| 4 | find and find_oneOne document against a cursor you can walk only once, and a projection that keeps it small | Open in ColabReadSolutions |
| 5 | Query OperatorsWhy {'size': {'w': 1}} matches nothing when {'size.w': 1} matches every document where w is 1 | Open in ColabReadSolutions |
| 6 | Update Operators$set against the replacement that dropped every other field, and the upsert that inserted a second copy | Open in ColabReadSolutions |
| 7 | Bulk Writes and Transactionsbulk_write, ordered against unordered, and the write you forgot to pass the session to | Open in ColabReadSolutions |
| 8 | IndexesCompound order, explain, $text, and the unique index that refuses to build | Open in ColabReadSolutions |
| 9 | The Aggregation Pipeline$match, $group, $unwind, and the stage order that decides how long it runs | Open in ColabReadSolutions |
| 10 | Modeling Without JoinsEmbed until the document will not fit, and the $lookup that scans because the foreign field has no index | Open in ColabReadSolutions |
| 11 | Beanie DocumentsA Pydantic model that is also a collection, init_beanie, and the await a Colab cell already allows | Open in ColabReadSolutions |
| 12 | Async Queriesfind, to_list, beanie.operators, and the projection model that will not validate | Open in ColabReadSolutions |
| 13 | Saving Changesset and inc against save, save_changes, and the revision that moved under you | Open in ColabReadSolutions |
| 14 | Link and BackLinkLink, BackLink, fetch_links, and the single $lookup pipeline that saves a round trip per document | Open in ColabReadSolutions |
| 15 | Migrationsbeanie new-migration, iterative against free fall, and the replica set beanie migrate assumes | Open in ColabReadSolutions |
| 16 | A Product CatalogOne real job: loaded with PyMongo, indexed for the queries it serves, typed with Beanie | Open in ColabReadSolutions |