0Pricing
Node.js Backend Development Bootcamp · Lesson

Querying & Filtering Data with Mongoose

Go beyond basic CRUD and learn how to build powerful queries in Mongoose using filters, operators, sorting, pagination, and projections.

Querying & Filtering Data with Mongoose is a free Node.js Backend Development Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Node.js Backend Development Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond Find-All

Fetching every document with Model.find() works for tiny collections, but real apps need to filter, sort, limit, and shape their results.

Mongoose gives you a fluent query API that mirrors MongoDB's query language.

const users = await User.find({ active: true });

Filtering by Field

Pass an object to find() to match documents by exact field values. Multiple keys are combined with logical AND.

const admins = await User.find({ role: 'admin', active: true });

Comparison Operators

MongoDB operators start with $. The most common comparisons are:

  • $gt / $gte greater than (or equal)
  • $lt / $lte less than (or equal)
  • $ne not equal
const adults = await User.find({ age: { $gte: 18 } });

Matching Multiple Values

Use $in to match any value from a list, and $nin to exclude a list. This is cleaner than chaining many OR conditions.

const team = await User.find({ role: { $in: ['admin', 'editor'] } });

Logical OR

The $or operator takes an array of conditions; a document matches if any one is true.

const results = await User.find({
  $or: [{ city: 'Berlin' }, { city: 'Paris' }]
});

Sorting Results

Chain .sort() to order results. Use 1 for ascending and -1 for descending. You can sort by multiple fields.

const recent = await User.find().sort({ createdAt: -1, name: 1 });

Limiting and Skipping

Pagination relies on .limit() (max documents) and .skip() (how many to bypass). For page 2 with 10 per page, skip 10 and limit 10.

const page = 2, perPage = 10;
const items = await User.find()
  .skip((page - 1) * perPage)
  .limit(perPage);

Selecting Specific Fields

Use .select() (projection) to return only the fields you need. Prefix a field with - to exclude it. This reduces payload size and hides sensitive data.

const safe = await User.find().select('name email -_id');
const noPass = await User.find().select('-password');

Finding a Single Document

findOne() returns the first match or null. findById() is a shortcut for matching by _id.

const user = await User.findOne({ email: 'a@b.com' });
const byId = await User.findById('652f1c...');

Counting Documents

To know how many documents match a filter without fetching them all, use countDocuments(). This is essential for showing total pages in pagination.

const total = await User.countDocuments({ active: true });
console.log('Active users:', total);

Text & Regex Search

For partial matches, use a regular expression. The $regex operator with the i option makes a case-insensitive search.

const matches = await User.find({
  name: { $regex: 'an', $options: 'i' }
});

Quick Check

Test your Mongoose querying skills.

Recap

You can now build rich Mongoose queries:

  • Filter by field and combine with operators like $gte, $in, $or
  • Order results with .sort()
  • Paginate with .skip() and .limit()
  • Shape output with .select()
  • Count matches with countDocuments()

These tools turn a basic data layer into a flexible, production-ready API.

Frequently asked questions

Is the “Querying & Filtering Data with Mongoose” lesson free?

Yes — the full text of “Querying & Filtering Data with Mongoose” is free to read here on the web, and the Node.js Backend Development Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Node.js Backend Development Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Querying & Filtering Data with Mongoose”?

Go beyond basic CRUD and learn how to build powerful queries in Mongoose using filters, operators, sorting, pagination, and projections. You practise Node.js Backend Development Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Node.js Backend Development Bootcamp?

No prior experience is required. Node.js Backend Development Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Querying & Filtering Data with Mongoose” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Node.js Backend Development Bootcamp lesson?

Yes. Every Node.js Backend Development Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Connecting Node.js to MongoDB
  2. Mongoose ODM for Data Modeling
  3. CRUD Operations with Mongoose
  4. Querying & Filtering Data with Mongoose
← Back to Node.js Backend Development Bootcamp