Querying Documents

A MongoDB 1,815 query is a document. You describe the shape you are looking for — { year: { $gte: 1980 } } — and the server returns every document that matches: no query string to parse, no injection-prone concatenation, and the same object literal works in mongosh 403 , in the driver, in $match (The Aggregation Pipeline) and in Mongoose 243,355 .

CRUD Operations showed how to run find() and how a cursor delivers results. This section is about what goes inside the filter: the operator families, how they treat missing fields and mixed types, and the places where the obvious query quietly returns the wrong set. Every example below runs against the same eight-document books collection on MongoDB 8.3.

The sample collection used throughout Section 4.4
db = db.getSiblingDB('bookstore');
db.books.insertMany([
{ _id: 1, title: 'Dune', author: { name: 'Frank Herbert', country: 'US' }, year: 1965, price: 12.99,
  stock: 34, tags: ['scifi', 'classic'], ratings: [5, 4, 5, 3], editions: [
  { format: 'paperback', year: 2005, pages: 688 },
  { format: 'hardcover', year: 2018, pages: 704 } ] },
{ _id: 2, title: 'Neuromancer', author: { name: 'William Gibson', country: 'CA' }, year: 1984,
  price: 9.5, stock: 0, tags: ['scifi', 'cyberpunk'], ratings: [4, 4, 5],
  editions: [ { format: 'paperback', year: 2000, pages: 271 } ] },
{ _id: 3, title: 'The Hobbit', author: { name: 'J. R. R. Tolkien', country: 'UK' }, year: 1937,
  price: 14.0, stock: 12, tags: ['fantasy', 'classic'], ratings: [5, 5, 5, 4, 5], editions: [
  { format: 'hardcover', year: 2012, pages: 320 },
  { format: 'ebook', year: 2020, pages: 310 } ] },
{ _id: 4, title: 'Snow Crash', author: { name: 'Neal Stephenson', country: 'US' }, year: 1992,
  price: '11.25', stock: 7, tags: ['scifi', 'cyberpunk'], ratings: [4, 3, 5],
  editions: [ { format: 'paperback', year: 2003, pages: 440 } ] },
{ _id: 5, title: 'Piranesi', author: { name: 'Susanna Clarke', country: 'UK' }, year: 2020,
  price: 16.5, stock: 21, tags: ['fantasy'], ratings: [5, 4], discount: null,
  editions: [ { format: 'hardcover', year: 2020, pages: 245 } ] },
{ _id: 6, title: 'Project Hail Mary', author: { name: 'Andy Weir', country: 'US' }, year: 2021,
  price: 18.75, stock: 40, tags: ['scifi'], ratings: [5, 5, 4, 5], editions: [
  { format: 'hardcover', year: 2021, pages: 476 },
  { format: 'ebook', year: 2021, pages: 470 } ] },
{ _id: 7, title: 'Kindred', author: { name: 'Octavia E. Butler', country: 'US' }, year: 1979,
  price: 13.25, stock: 5, tags: ['scifi', 'classic'], ratings: [5, 4, 4],
  editions: [ { format: 'paperback', year: 2004, pages: 287 } ] },
{ _id: 8, title: 'Ancillary Justice', author: { name: 'Ann Leckie', country: 'US' },
  year: 2013, price: 15.0, stock: 0, tags: [], ratings: [], editions: [] }]);

Three documents are deliberately irregular, and they drive most of the traps below: Snow Crash stores its price as the string '11.25', the damage a sloppy import leaves behind; Piranesi has discount: null while the other seven have no discount field at all; and Ancillary Justice carries empty arrays.

Subsections