Reading and Changing Data
Ask MongoDB for exactly what you need: filters with comparison operators, one document by its id, only the fields you want, sorted and a page at a time. Then change documents in place with update operators, delete them, and make searches fast with indexes and text search.
Updated
Finding Documents
- Finding Documents with Filters (Apprentice): A filter object tells find and findOne which documents you want, with operators such as $gte and $in for comparisons, dot notation for a field inside a field, an ObjectId to find one document by its id, and a projection to send back only the fields you need.
- Sorting and Paging Results (Journeyman): sort puts a query's results in order, skip and limit cut out one page of them, and countDocuments counts every match, so an API can answer with page 2 of 3 instead of every document at once.
Changing Documents
- Updating and Deleting Documents (Apprentice): updateOne and updateMany change documents in place with update operators such as $set, $inc, $push and $pull, their result's matchedCount and modifiedCount say what happened, findOneAndUpdate gives back the changed document, and deleteOne removes one.
Fast Searches
- Indexes and Text Search (Journeyman): An index lets MongoDB jump to the documents a query wants instead of reading every one, a unique index refuses a second document with the same value, and a text index lets $text search the words in a collection's text fields.
Exercise: Farmers Market Vendors
- Exercise: Farmers Market Vendors (Journeyman): Write the Saturday Farmers Market's vendor functions with the official driver (filters with $in and $gte, a sorted page with its total, one vendor by its id, updates with $set and $push, a delete, two indexes and a text search) and let tests on a throwaway MongoDB check every one.