Stage 2 of 8 · 9 lessons · ~2 weeks
Reading and writing documents
Insert, find, update and delete documents in mongosh: filter with query operators, shape results with projection, sort and limit, change fields with update operators, and know when to upsert or replace.
Start: Inserting documentsLessons· one lesson for each topic, in this order
- 1Inserting documentsLesson
- 2find, findOne and equality filtersLesson
- 3Comparison and logical query operatorsLesson
- 4Projection: choosing the fields you get backLesson
- 5Sorting, skip, limit and cursorsLesson
- 6updateOne, updateMany and update operatorsLesson
- 7UpsertsLesson
- 8Replacing versus updatingLesson
- 9Deleting documents safelyLesson
Do this
- Insert single documents and a batch, and read the returned ids.
- Write filters with ranges, $in, $or, $exists and $ne, predicting the count first.
- Add projections to every list query you write.
- Write a paginated query with sort, skip, limit and a tie-breaker.
- Update fields with $set, $inc and $unset and check matchedCount and modifiedCount.
- Write an upsert with $setOnInsert.
- Run the count-first routine before a deleteMany.
Start with
- CourseFreeMongoDB University
MongoDB
Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs
Practise
Keep handy
Before moving on: A saved mongosh script of 30 working commands against your sample dataset, each with a comment stating the question it answers or the change it makes, including range filters, a projection, a paginated query, update operators, an upsert and a counted delete.

Before you run a query, guess how many documents it will return, and before you run an update or delete, count what the filter matches. Predicting and then checking catches more bugs than any tool, and it is the habit that stops you wiping a collection on a Friday afternoon.