Lesson 19/100

Tutorials MongoDB Tutorial

Query Filters

Query Filters: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.

On this page

MongoDB Tutorial · Lesson 19 of 100

Query Filters

Foundations & CRUDQueries & SchemaAggregation & ScaleAtlas & Projects

Foundations & CRUD · 1 — Documents · ~6 min · MongoDB — CRUD Operations

What is this?

A query filter is the object you pass to find/update/delete. Equality is { field: value }. Operators use $ prefix. Nested fields use dot notation like address.city.

Why should you care?

Wrong filters return wrong carts, wrong tenants, or leak data across users. Filters are your access logic in the database layer.

See it live — copy this example

Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.

db.orders.insertMany([
  { userId: 9, status: "placed", total: 1200, address: { city: "Pune" } },
  { userId: 9, status: "shipped", total: 800, address: { city: "Pune" } },
  { userId: 3, status: "placed", total: 500, address: { city: "Goa" } }
])
db.orders.find({
  userId: 9,
  status: { $in: ["placed", "shipped"] },
  "address.city": "Pune",
  total: { $gte: 1000 }
})

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • AND combines all top-level conditions.
  • $in allows two statuses.
  • Dot notation reaches city inside address.
  • $gte keeps totals ≥ 1000 — only the first order matches.

Practice next

  1. Insert the sample orders.
  2. Run the filter and confirm one match.
  3. Remove the total condition and see two Pune orders for user 9.
  4. Add $or for city Pune or Goa.
  5. Filter status: "placed" only.

Remember

Filters are plain objects with fields and $operators. Dot notation reaches nested fields. Multiple fields mean AND.

Tenant-safe order history

API always includes tenantId and userId in the filter so Customer A never sees Customer B.

Outcome: Authorization is enforced at query time, not only in the UI.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MongoDB.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define SQL queries in plain languag…
Mid Detailed
What are common mistakes teams make with Schema design when using MongoDB?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Schema design in plain langu…
Senior Detailed
How would you debug a production issue related to Transactions in a MongoDB application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Transactions in plain langua…
Mid Detailed
Compare two approaches to Indexing—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Indexing in plain language f…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MongoDB project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Normalization in plain langu…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MongoDB Tutorial
Course syllabus

MongoDB Tutorial

MongoDB — Foundations
MongoDB — CRUD Operations
MongoDB — Query Operators
MongoDB — Schema Design
MongoDB — Indexing & Performance
MongoDB — Aggregation Pipelines
MongoDB — Replication & Sharding
MongoDB — Atlas & Security
MongoDB — Modern Features
MongoDB — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details