How to find length of the array mongodb

2020-03-05 09:57发布

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",
                    }
                ]
            }
        }

1条回答
一纸荒年 Trace。
2楼-- · 2020-03-05 10:40

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

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

MongoPlayground

查看更多
登录 后发表回答