Skip to content Skip to sidebar Skip to footer

How To Get Data Mongo Data Using Aggregate Mongodb

Suppose we have 10 collection, then we have to find the count on the basis of tag_id. For example, if tag_id contains 0 and 1, then we have to count all the data, as well as counti

Solution 1:

You can use below aggregation pipeline.

The below query will $unwind the tag_id followed by $group to count email and $cond operator to count the unread email.

db.collection.aggregate(
 {$unwind:{path:"$tag_id", preserveNullAndEmptyArrays:true}}, 
 {$group:{
    _id:"$tag_id", 
     count_email:{$sum:1},
     unread:{$sum:{$cond:[{$eq:["$unread", "false"]}, 0, 1]}}
    }
  }
);

Post a Comment for "How To Get Data Mongo Data Using Aggregate Mongodb"