Course contentsShow
← System Design
Lesson 393 of 1,91911. NoSQL: Document StoresFree lesson

MongoDB Query Language Basics

Using MongoDB's query operators for filtering, projection, and matching documents within collections.

MongoDB Query Language Basics

What you'll learn: How to use MongoDB's query operators to find, filter, and shape the documents you need from collections.

MongoDB's query language lets you filter documents (find what matches), project fields (choose what to return), and match patterns within your data—all using a JSON-like syntax that mirrors the document structure itself.

Core Query Operations

Unlike SQL's SELECT ... WHERE ..., MongoDB uses query operators inside JavaScript-like objects. The basic pattern is:

db.collection.find(filterCriteria, projectionOptions)

Filtering with Query Operators

Comparison operators let you match values:

// Find users older than 25
db.users.find({ age: { $gt: 25 } })

// Find active premium users
db.users.find({ status: "active", plan: "premium" })

Common operators: $eq (equal), $gt (greater than), $lt (less than), $in (matches any value in array), $ne (not equal).

Projection: Choosing Fields

Like selecting columns in SQL, projection controls which fields appear in results:

// Return only name and email (plus _id by default)
db.users.find({ age: { $gt: 25 } }, { name: 1, email: 1 })

// Exclude address field
db.users.find({}, { address: 0 })

Think of 1 as "include this" and 0 as "exclude this."

Matching Nested Documents

Since documents can contain embedded documents, you query nested fields with dot notation:

// Find users in New York
db.users.find({ "address.city": "New York" })

This queries the city field inside the embedded address document—powerful for hierarchical data.

Key Takeaway: MongoDB's query language uses JSON-like operators ($gt, $in, etc.) for filtering, numeric flags for projection, and dot notation for nested fields, giving you flexible, expressive document queries without SQL.