Querying Documents with find()

beginner
12 min

Querying Documents with find()

Reading data is the operation your application will perform most, and in MongoDB it revolves around one method: find(). In this CodeYourCraft lesson you will learn how to filter documents, shape results with projections, sort, paginate, and count, all from the mongosh shell.

find() and findOne()

Called with no arguments, find() returns every document in a collection as a cursor, while findOne() returns just the first matching document:

javascript
use shop db.products.find() // all products db.products.findOne() // a single document or null

In mongosh, a cursor automatically prints the first 20 results, and typing it shows the next batch. In application code you iterate the cursor or convert it to an array.

Filtering with Query Documents

The first argument to find() is a filter, itself written as a document. A simple filter matches fields by equality:

javascript
db.products.find({ price: 2999 }) db.products.find({ name: 'Desk Lamp' })

Multiple fields in one filter act as an implicit AND:

javascript
db.products.find({ price: 2999, tags: 'video' })

Notice that querying tags with a plain string matches any array that contains that value. Array containment queries come free in MongoDB.

Querying Nested Fields with Dot Notation

To reach inside embedded documents, use dot notation and quote the path:

javascript
db.products.find({ 'stock.warehouse': { $gte: 100 } }) db.customers.find({ 'address.city': 'Pune' })

Dot notation also works for array positions, so 'tags.0' refers to the first tag.

Projections: Choosing Fields to Return

The second argument to find() is a projection that trims the returned documents, saving bandwidth and memory. Use 1 to include fields and 0 to exclude them:

javascript
db.products.find( { price: { $lt: 5000 } }, { name: 1, price: 1, _id: 0 } ) // [ { name: 'USB-C Cable', price: 499 }, { name: 'Webcam 1080p', price: 2999 }, ... ]

You cannot mix inclusion and exclusion in one projection, with one exception: _id, which is included by default, may always be excluded.

Sorting, Skipping and Limiting

Cursors support chainable modifiers that MongoDB applies server-side:

javascript
db.products.find({}, { name: 1, price: 1, _id: 0 }) .sort({ price: -1 }) // most expensive first (1 = ascending, -1 = descending) .skip(10) // skip the first 10 .limit(5) // return at most 5

This trio is the classic recipe for pagination, though for large offsets a range-based approach (filtering on the last seen _id or a sort key) is faster because skip still walks the skipped documents.

Counting Results

Use countDocuments for accurate counts with a filter, and estimatedDocumentCount for a fast collection total based on metadata:

javascript
db.products.countDocuments({ price: { $lt: 5000 } }) db.products.estimatedDocumentCount()

Key Takeaways

  • find() returns a cursor; findOne() returns a single document.
  • Filters are documents; multiple fields form an implicit AND.
  • Matching a scalar against an array field checks containment automatically.
  • Dot notation reaches nested fields and array positions.
  • Projections shape results; sort, skip, and limit run on the server.

Simple equality only gets you so far. In the next lesson we unlock comparison, logical, and element query operators for far more expressive filters.

Querying Documents with find() - MongoDB | CodeYourCraft | CodeYourCraft