Looping through array of objects using mongoDB aggregation to count number of documents after checking conditions

RoDev

I am trying to write aggregation query in mongoDb where I want to loop over an array of objects and get count of those objects which meet certain conditions. I have array named banks[] which has 28-30 objects in it. I want to check whether "pdf" key exists in every object and check whether the value is true or false and return only count of those objects having value true. How can I do it using aggregation? Please help. Below is sample data in banks[] array.

banks[{id:1, name: "ABC","pdf": true},{id:2, name:"PQR"}, {id:3, name:"XYZ", pdf:"false"}....]

Similarly the array has 30 objects.

The output which I want for above sample is

{id:"1", count:1}

Thanks in advance!

varman

You can use $size to get the count of an array. $filter is used to filter based on conditions.

{
    "$project": {
      count: {
        $size: {
          $filter: {
            input: "$banks",
            cond: {
              $eq: [
                "$$this.pdf",
                true
              ]
            }
          }
        }
      }
    }
  }

Working Mongo playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MongoDB get the number of documents (count) in a collection using Node.js

get averages using mongodb aggregation from array containing objects

looping through array document in mongodb

How to refactor code for looping through array of objects using react?

Add field to documents after $sort aggregation pipeline which include its index in sorted list using MongoDb aggregation

Checking previous elements of array when looping through

Aggregation in MongoDB with Array of objects

Looping through JSON objects in Array

MongoDB Aggregation - Query array of objects

How to count specific documents in a row using aggregation?

Count number of documents using Aggregation MongoDB

looping through a string and checking if certain conditions are met

Looping through objects in an array JS

Looping Array and checking MongoDB collection in loop (Async)

Order by Array of documents in aggregation mongodb

How do i count number of referals using aggregation in mongoDB

Looping through multidimensional array and checking conditions

Looping through an array of objects successfully

Angular Typescript looping through array of objects to find the count

Looping through array of objects PHP

mongoDB, mongoose - aggregation an array of objects

Ruby looping through two objects and checking if something is present in both objects

Looping through array to count in mongodb/mongoose

Looping through an array and checking multiple conditions

Looping through nested array of objects

MongoDB Aggregation - match documents with array of objects, by another array of objects filter

Looping through an array of objects and filter using a key

How To Count Values Inside Deeply Nested Array Of Objects in Mongodb Aggregation

MongoDB Aggregation Match in array of objects

TOP Ranking

HotTag

Archive