Prateek Sharma
Prateek Sharma

Reputation: 39

How to count number of false values in an array mongo db query

_id:5e4d18bd10e5482eb623c6e4
notification_obj:
 0  notification_text:"Welcome to the app and your account is created hello."
    open:false
    type:"just_click"

 1  notification_text:"Sebal started following you."
    open:true
    type:"open_profile"

 2  notification_text:"Hella started following you."
    open:false
    type:"open_profile"

So here I have an array 'notification_obj' array in a document of mongo database, I want to search the record with _id and in that record I want to count 'How many 'open:false' values are there. I want to count in this array that open:false how many times. Please help with a "query" in mongo db.

Upvotes: 2

Views: 361

Answers (1)

Mayur Vaghasiya
Mayur Vaghasiya

Reputation: 1482

I think this code help you.

db.getCollection('your_collection').aggregate([
  {
    $match: { _id: ObjectId("5a544.............") }
  },
  {
    $unwind: '$notification_obj'
  },
  {
    $match: { 'notification_obj.open': false }
  },
  {
    $count: 'total'
  }
]);

Output:

{
    "total" : 1
}

Upvotes: 4

Related Questions