MongoDB

Stage 2 · Lesson 2 of 9

find, findOne and equality filters

Reading and writing documents

find(filter) returns every document that matches the filter, and findOne(filter) returns the first match or . A filter is itself a document: { status: "shipped", country: "PK" } matches documents where both are equal to those values, so listing several fields means AND.

An empty filter, {}, matches everything. Matching is exact and case sensitive, and type matters: the string "5" does not equal the number 5. A filter on a field that some documents lack simply skips them.

countDocuments(filter) counts matches without fetching them. Filtering on the server rather than in your code means only the matching documents travel over the network, and can help later.

Common trapfiltering in application code after find({}) returns the whole collection.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • CourseFree
    MongoDB University

    MongoDB

    Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs

Practise

  • Test filters and updates on your own sample documents and share the link when you get stuck

Keep handy

Mark this stage as not done?