Dhananjaya
Dhananjaya

Reputation: 33

Mongodb aggregation pipeline stages

I have an aggregation of say 4 stages match,lookup, unwind, project as mentioned in the same order.

Suppose if the resultant data from the match stage is null that is it does not return any document then does the aggregation breaks? or it passes null data to next stage and executes all the next stages?

If it executes all the next stage then how to break the aggregation when the result of match query is null.

I am asking this so that i can minimise the query to the database.

Upvotes: 0

Views: 1565

Answers (1)

Gibbs
Gibbs

Reputation: 22956

To answer your question, if it is null, it will execute the stages and returns empty result. You don't need to break it if it is null.

Play

The MongoDB aggregation pipeline consists of stages. Each stage transforms the documents as they pass through the pipeline.

Reference

Explanation:

db.orders.aggregate([
   { $match: { status: "A" } },
   { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])

First Stage: The $match stage filters the documents by the status field and passes to the next stage those documents that have status equal to "A".

Second Stage: The $group stage groups the documents by the cust_id field to calculate the sum of the amount for each unique cust_id.

Upvotes: 1

Related Questions