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",
                    }
                ]
            }
        }
5
  • Possible duplicate of MongoDB: count the number of items in an array Commented May 12, 2019 at 18:23
  • Year is dynamic? 2017 Commented May 12, 2019 at 18:24
  • I did what is mentioned in that post but can't achieve results, that's why I posted new question. Commented May 12, 2019 at 18:24
  • @AnthonyWinzlet No. Commented May 12, 2019 at 18:24
  • @yashChoksi You will have to be more specific than "can't achieve results". Commented May 12, 2019 at 18:30

1 Answer 1

10

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

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

MongoPlayground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.