检查并计算mongodb中子数组内字段的存在

时间:2021-01-19 02:54:42
{
    "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?

每次出现的关键“文本”都会被视为注释(因此有4条注释)。在Mongo中,最好的方法是什么?

1 个解决方案

#1


0  

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

基本上你必须使用$ map循环遍历每个数组,并计算Text不等于$ 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
  }
]

#1


0  

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

基本上你必须使用$ map循环遍历每个数组,并计算Text不等于$ 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
  }
]