Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Cheat sheet · Foundations

MongoDB cheat sheet (JavaScript)

Opens with credits

For students and early-career engineers learning MongoDB from JavaScript, from the first document to data modelling, indexes, aggregation and transactions. Shell snippets are mongosh; application code uses the Node.js driver and Mongoose.

Curated by Tauseef Fayyaz8 chapters77 concepts171 glossary terms

Chapters

8 chapters · 77 concepts

  1. 1Documents and your setupWhat a document database is, how MongoDB stores documents in BSON inside collections, and the cluster, shell, GUI and sample data you need before writing real queries.8 concepts
  2. 2Reading and writing documentsInserting, finding, updating and deleting documents: filters and query operators, projections, sorting and paging, update operators, upserts and cursors.10 concepts
    1. 1insertOne and insertManyinsertOne adds one document and returns its _id; insertMany adds an array of documents in one round trip.
    2. 2find and query filtersfind takes a filter document and returns every matching document; findOne returns the first match.
    3. 3Query operatorsOperators starting with $ express conditions: $eq, $ne, $gt, $gte, $lt, $lte, $in and $nin compare values, $and, $or and $not combine conditions, $exists and…
    4. 4ProjectionThe second argument to find, listing the fields to return (1) or leave out (0). _id is returned unless you exclude it.
    5. 5sort, skip and limitCursor methods that order results by one or more fields (1 ascending, -1 descending), skip a number of documents and cap how many come back.
    6. 6Update operatorsupdateOne and updateMany take a filter and an update document of operators: $set sets fields, $unset removes them, $inc adds to a number, $currentDate stamps…
    7. 7UpsertAn update with upsert: true inserts a new document when nothing matches the filter, built from the filter and the update. $setOnInsert sets fields only on that…
    8. 8replaceOne versus updateOnereplaceOne swaps the whole document, apart from _id, for the one you pass. updateOne changes only the fields named by its operators and refuses a plain…
    9. 9deleteOne and deleteManydeleteOne removes the first matching document and deleteMany removes every match. drop removes the whole collection with its indexes.
    10. 10Cursorfind returns a cursor, a pointer to the result that fetches documents from the server in batches as you iterate. mongosh prints the first 20; type it for more.
    10 in this chapterOpen this chapter
  3. Arrays and nested documentsQuerying and updating fields inside embedded documents and arrays: dot notation, array matching, $elemMatch, array update and positional operators, and the document size limit.6 conceptsOpens with credits
  4. Data modelling and schema designShaping documents around how the app reads and writes them: embedding versus referencing, relationship sizes, the common schema design patterns, schema validation and the anti-patterns to avoid.12 conceptsOpens with credits
  5. Indexes and query plansHow indexes make reads fast, which kind to create for which query, how to read explain output, and the habits that keep queries quick as data grows.10 conceptsOpens with credits
  6. The aggregation pipelineTransforming and summarising documents in stages: filtering, grouping, unwinding arrays, joining with $lookup, multi-facet results, writing results out, the pipeline limits and views.11 conceptsOpens with credits
  7. MongoDB from your codeUsing MongoDB safely from an application: the driver, connection strings and pooling, schemas in code, injection, duplicate key errors, pagination, change streams and retryable writes.9 conceptsOpens with credits
  8. Transactions, replication and operationsHow MongoDB keeps data durable and available: write and read concerns, read preference, replica sets, multi-document transactions, sharding, access control, network access, backups and monitoring.11 conceptsOpens with credits
  9. 9Glossary, A to ZEvery concept in the sheet and the smaller terms it uses, in one list.171 terms

About this sheet

Documents, modelling, indexes, aggregation.

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com