querydrill

Learn ›Aggregation ›The aggregation pipeline

find() vs aggregate()

find()$group$gte$match$sum

Before touching aggregation, it is worth being precise about what find() already does, because a lot of people reach for a pipeline they do not need - and a lot of others try to force find() to do something it structurally cannot.

Use find() when you’re basically:

Filter documents
Sort
Project fields
Paginate

Example:

db.users.find(
  {
    status: "active",
    age: { $gte: 25 }
  },
  {
    name: 1,
    email: 1
  }
)
.sort({
  createdAt: -1
})
.limit(10)

Use aggregate() when you’re doing:

Grouping
Calculations
Transformations
Joining collections
Complex array manipulation
Analytics/reporting
Multi-stage processing

Example:

db.orders.aggregate([
  {
    $match: {
      status: "completed"
    }
  },
  {
    $group: {
      _id: "$userId",
      totalSpent: {
        $sum: "$amount"
      }
    }
  }
])

Don’t worry about understanding every part yet.

Just notice:

find()

Document → Filter → Return documents

vs:

aggregate()

Documents
   ↓
Stage 1
   ↓
Stage 2
   ↓
Stage 3
   ↓
Transformed result

Aggregation is a pipeline.