Learning MongoDB

0% completed

Previous
Next
Mongodb Aggregation $count

The $count stage in MongoDB's aggregation framework is used to count the number of documents that pass through the pipeline. It is a simple and efficient way to obtain the count of documents that match specific criteria. This stage is often used in reporting and data validation scenarios to quickly tally the number of documents that meet certain conditions.

Syntax

{ $count: "<field>" }
  • $count: The operator used to specify the counting stage.
  • field: The name of the field to store the count.

Example Setup

First, let's insert some documents into the orders collection to work with:

db.orders.insertMany([ { order_id: 1, product: "apple", quantity: 10, status: "completed" }, { order_id: 2, product: "banana", quantity: 5, status: "pending" }, { order_id: 3, product: "orange", quantity: 8, status: "completed" }, { order_id: 4, product: "apple", quantity: 15, status: "pending" }, { order_id: 5, product: "banana", quantity: 7, status: "completed" } ])

Example 1: Basic Usage of $count

Count the number of documents in the orders collection.

Pipeline:

db.orders.aggregate([ { $count: "totalOrders" } ])

Explanation:

  • $count: This stage counts the total number of documents in the collection and stores the count in a field named totalOrders.

This command results in an output like:

{ "totalOrders": 5 }

Example 2: Counting Documents with a Specific Condition

Count the number of completed orders.

Pipeline:

db.orders.aggregate([ { $match: { status: "completed" } }, { $count: "completedOrders" } ])

Explanation:

  • $match: Filters the documents to include only those where the status is "completed".
  • $count: Counts the number of documents that match the condition and stores the count in a field named completedOrders.

This command results in an output like:

{ "completedOrders": 3 }

Example 3: Counting After Grouping

Count the number of different products ordered.

Pipeline:

db.orders.aggregate([ { $group: { _id: "$product" } }, { $count: "uniqueProducts" } ])

Explanation:

  • $group: Groups the documents by the product field, resulting in distinct groups for each product.
  • $count: Counts the number of unique groups (products) and stores the count in a field named uniqueProducts.

This command results in an output like:

{ "uniqueProducts": 3 }

Use Cases

  1. Data Validation: Verify the number of documents that meet certain criteria, such as counting the number of completed orders to ensure data integrity.
  2. Performance Monitoring: Monitor the count of documents processed in various stages of data workflows, such as counting the number of new orders received each day.
  3. Analytics: Perform simple analytics by counting documents that match specific conditions, such as determining the number of unique products ordered.

The $count stage is a straightforward yet powerful tool in MongoDB's aggregation framework. It allows you to quickly count the number of documents that pass through the pipeline, making it useful for data validation, reporting, and simple analytics.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next