8
{
    "ArticleName": "Example Article",
    "Comments": [
        {
            "Text": "Great Article",
            "Responses": [
                {
                    "Text": "No it isnt",
                    "Responses": [
                        {
                            "Text": "Yes it is"
                        }
                    ]
                },
                {
                    "Text": "Spot on"
                }
            ]
        }
    ]
}

Every occurrence of the key 'Text' would be considered as a comment (so 4 comments). What's the best way to get a count on this in Mongo?

7
  • Is there any issue with the below answer Commented Nov 25, 2018 at 13:58
  • @AnthonyWinzlet I've rolled the question back to before you edited it. It needs to be a count on an infinite (hence the title) number of comments not just the ones I've shown in the example. So it needs to be a dynamic solution Commented Nov 27, 2018 at 12:10
  • It will count any number of comments. Check the mongoplayground link in the answer and edit your collection in the left hand side of the playground. It will count all the nested comments. Commented Nov 27, 2018 at 12:14
  • You can check here. I have added more nested comments. Now count changed Commented Nov 27, 2018 at 12:23
  • So it does @AnthonyWinzlet. Very good. I'll mark that correct. I just thought there'd be a nicer built in function to grab all this. I've since switched to a graph db but good to know Commented Nov 27, 2018 at 12:23

1 Answer 1

1

You can try below aggregation

Basically you have to loop over the each array using $map and count for the fields where Text is not equal to $ne undefined

db.collection.aggregate([
  { "$project": {
    "commentsCount": {
      "$sum": {
        "$map": {
          "input": "$Comments",
          "as": "cc",
          "in": {
            "$add": [
              { "$cond": [{ "$ne": [ "$$cc.Text", undefined ] }, 1, 0 ] },
              { "$sum": {
                "$map": {
                  "input": "$$cc.Responses",
                  "as": "dd",
                  "in": {
                    "$add": [
                      { "$cond": [{ "$ne": [ "$$dd.Text", undefined ] }, 1, 0 ] },
                      { "$sum": {
                        "$map": {
                          "input": "$$dd.Responses",
                          "as": "ee",
                          "in": { "$cond": [{ "$ne": [ "$$ee.Text", undefined ] }, 1, 0 ] }
                        }
                      }}
                    ]
                  }
                }
              }}
            ]
          }
        }
      }
    }
  }}
])

Output

[
  {
    "commentsCount": 4
  }
]
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.