7

I have such this type of structure of my database. I want to find number of documents my "JAN" array contains. I am trying to do in different way but nothing works. That would be great if someone helps.

 {
            "_id" : ObjectId("5cd8609db20663a19e2c362c"),
            "2017" : {
                "JAN" : [
                    {
                        "SYMBOL" : "20MICRONS",
                        "SERIES" : "EQ",
                        "OPEN" : "33.4",
                        "HIGH" : "34.3",
                        "LOW" : "33",
                    },
                    {
                        "SYMBOL" : "3IINFOTECH",
                        "SERIES" : "EQ",
                        "OPEN" : "5.8",
                        "HIGH" : "5.8",
                        "LOW" : "5.55",
                    },
                    {
                        "SYMBOL" : "3MINDIA",
                        "SERIES" : "EQ",
                        "OPEN" : "11199.9",
                        "HIGH" : "11233",
                        "LOW" : "10861",
                    }
                ]
            }
        }
Ashh
  • 44,693
  • 14
  • 105
  • 132
Yash Choksi
  • 2,132
  • 3
  • 9
  • 18

1 Answers1

7

You can use $size operator to find the length of the array

db.collection.aggregate([
  {
    "$project": {
      "totalJan": {
        "$size": "$2017.JAN"
      }
    }
  }
])

MongoPlayground

Ashh
  • 44,693
  • 14
  • 105
  • 132