codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

13 / 65 topics
12Querying Basics13Find Queries14Projection15Sorting and Limiting16Aggregation Framework17Operators in MongoDB18Text Search
Tutorials/MongoDB/Find Queries
🍃MongoDB

Find Queries

Updated 2026-04-20
3 min read

Introduction

In this section, we will delve into one of the most fundamental operations in MongoDB: finding documents. The find() method is used to retrieve documents from a collection based on specified criteria. This tutorial will cover various aspects of using find(), including basic syntax, filtering, sorting, projection, and aggregation.

Basic Syntax

The basic syntax for the find() method is as follows:

db.collection.find(query, projection)
  • query: A document that specifies the selection criteria using query operators.
  • projection: Optional. Specifies which fields to include or exclude in the returned documents.

Example

Let's start with a simple example. Assume we have a collection named users with the following documents:

{ "_id" : 1, "name" : "Alice", "age" : 25 }
{ "_id" : 2, "name" : "Bob", "age" : 30 }
{ "_id" : 3, "name" : "Charlie", "age" : 35 }

To find all documents in the users collection:

db.users.find()

This will return all three documents.

Filtering Documents

You can filter documents by specifying criteria in the query parameter. MongoDB provides a variety of query operators to perform complex queries.

Equality Operator

The equality operator ({ field: value }) is used to match documents where the field equals the specified value.

db.users.find({ name: "Alice" })

This will return:

{ "_id" : 1, "name" : "Alice", "age" : 25 }

Comparison Operators

MongoDB supports various comparison operators such as $gt, $lt, $gte, and $lte.

db.users.find({ age: { $gt: 30 } })

This will return:

{ "_id" : 3, "name" : "Charlie", "age" : 35 }

Logical Operators

Logical operators like $and, $or, and $not can be used to combine multiple conditions.

db.users.find({ $or: [{ name: "Alice" }, { age: 30 }] })

This will return:

{ "_id" : 1, "name" : "Alice", "age" : 25 }
{ "_id" : 2, "name" : "Bob", "age" : 30 }

Sorting Documents

The sort() method is used to sort the documents returned by a query.

db.users.find().sort({ age: -1 })

This will return:

{ "_id" : 3, "name" : "Charlie", "age" : 35 }
{ "_id" : 2, "name" : "Bob", "age" : 30 }
{ "_id" : 1, "name" : "Alice", "age" : 25 }

The 1 value sorts in ascending order, while -1 sorts in descending order.

Projection

Projection allows you to specify which fields to include or exclude in the returned documents. This can improve performance by reducing the amount of data transferred.

Including Fields

To include specific fields, set their values to 1.

db.users.find({}, { name: 1 })

This will return:

{ "_id" : 1, "name" : "Alice" }
{ "_id" : 2, "name" : "Bob" }
{ "_id" : 3, "name" : "Charlie" }

Excluding Fields

To exclude specific fields, set their values to 0.

db.users.find({}, { age: 0 })

This will return:

{ "_id" : 1, "name" : "Alice" }
{ "_id" : 2, "name" : "Bob" }
{ "_id" : 3, "name" : "Charlie" }

Excluding the _id Field

To exclude the _id field, set its value to 0.

db.users.find({}, { _id: 0 })

This will return:

{ "name" : "Alice", "age" : 25 }
{ "name" : "Bob", "age" : 30 }
{ "name" : "Charlie", "age" : 35 }

Aggregation

For more complex queries, MongoDB provides the aggregation framework. Aggregation pipelines allow you to process data and return computed results.

Example: Grouping Documents

Let's group users by age range:

db.users.aggregate([
  {
    $group: {
      _id: { $floor: { $divide: ["$age", 10] } },
      count: { $sum: 1 }
    }
  }
])

This will return:

{ "_id" : 2, "count" : 1 }
{ "_id" : 3, "count" : 1 }
{ "_id" : 4, "count" : 1 }

Best Practices

  1. Indexing: Ensure that your queries are optimized by creating indexes on frequently queried fields.
  2. Projection: Use projection to limit the amount of data returned, which can improve performance.
  3. Pagination: For large datasets, use pagination techniques like limit() and skip() to manage memory usage.
  4. Aggregation Framework: Use the aggregation framework for complex queries that involve multiple stages of processing.

Conclusion

In this tutorial, we covered the basics of finding documents in MongoDB using the find() method. We explored filtering, sorting, projection, and aggregation, providing real-world examples and best practices to help you effectively query your data. By mastering these concepts, you'll be well-equipped to perform efficient queries on your MongoDB collections.


PreviousQuerying BasicsNext Projection

Recommended Gear

Querying BasicsProjection