Mongodb 如何基于contentId获取数组计数

Mongodb 如何基于contentId获取数组计数,mongodb,mongodb-query,Mongodb,Mongodb Query,我有下面的mongodb结构 { "userId" : "5d3014fe56db690a5959a870", "contentId" : "1", "userAnswer" : { "uploads" : [ "123", "456"

我有下面的mongodb结构

    {
    "userId" : "5d3014fe56db690a5959a870",
    "contentId" : "1",
    "userAnswer" : {
        "uploads" : [
            "123",
            "456"
        ]
    }
},
{
    "userId" : "5d3014fe56db690a5959a8700",
    "contentId" : "1",
    "userAnswer" : {
        "uploads" : [
            "789"
        ]
    },

},
{
    "userId" : "5d3014fe56db690a5959a874",
    "contentId" : "2",
    "userAnswer" : {
    
        "uploads" : [
            null
        ]
    }
}
contentId
has(userAnswer.uploads)123&456&789。我想基于contentId进行计数,并将其推入一个新数组

我期待的答案

 {
   _id: 1,
   totalCount : 3
 },
 {
    _id: 2,
   totalCount : 0
 }
我的代码

db.test.aggregate([


{ $project : { "contentID":"$contentId", "regularStudent" : "$userAnswer.uploads"} },

{ $group : { _id : "$contentID", regularStudent: { $push: "$regularStudent" } } },

{ $project: { "regularStudent": { "$reduce": { "input": "$regularStudent", initialValue: [], "in": { $concatArrays: [ "$$this", "$$value" ] } } } } },

{ $project: { totalStudent: { $cond: { if: { $isArray: "$regularStudent" }, then: { $size: "$regularStudent" }, else: "NA"} } } }

])
我的输出

/* 1 */`enter code here`
{
    "_id" : "2",
    "totalStudent" : 1
},

/* 2 */
{
    "_id" : "1",
    "totalStudent" : 3
}
MongoDb版本3.4


我是新mongodb

[null]
不是空数组,它是一个包含1个元素的数组,因此它的大小实际上是1

如果需要从数组中消除
null
元素,请使用
$filter
,如

{ $project: { 
    totalStudent: { 
        $cond: { 
            if: { $isArray: "$regularStudent" }, 
            then: { 
                $size: {
                     $filter: {
                          input: "$regularStudent" 
                          cond: {$not:{$eq:["$$this", null]}}
                     }
                }
            }, 
            else: "NA"
         } 
    } 
} }

[null]
不是空数组,它是一个包含1个元素的数组,因此它的大小实际上是1

如果需要从数组中消除
null
元素,请使用
$filter
,如

{ $project: { 
    totalStudent: { 
        $cond: { 
            if: { $isArray: "$regularStudent" }, 
            then: { 
                $size: {
                     $filter: {
                          input: "$regularStudent" 
                          cond: {$not:{$eq:["$$this", null]}}
                     }
                }
            }, 
            else: "NA"
         } 
    } 
} }

有不同的方法来实现这个用例

我已经使用操作符投影了
userAnswer。上载了不带
null
值的
数组,然后可以使用
$size
返回正确的大小

db.collection.aggregate([
  {
    $project: {
      contentId: "$contentId",
      newArray: {
        $filter: {
          input: "$userAnswer.uploads",
          as: "u",
          cond: {
            $ne: [
              "$$u",
              null
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      contentId: 1,
      totalStudent: {
        $size: "$newArray"
      }
    }
  },
  {
    $group: {
      _id: "$contentId",
      totalStudent: {
        $sum: "$totalStudent"
      }
    }
  }
])

请参见此处的工作示例:

有不同的方法来实现此用例

我已经使用操作符投影了
userAnswer。上载了不带
null
值的
数组,然后可以使用
$size
返回正确的大小

db.collection.aggregate([
  {
    $project: {
      contentId: "$contentId",
      newArray: {
        $filter: {
          input: "$userAnswer.uploads",
          as: "u",
          cond: {
            $ne: [
              "$$u",
              null
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      contentId: 1,
      totalStudent: {
        $size: "$newArray"
      }
    }
  },
  {
    $group: {
      _id: "$contentId",
      totalStudent: {
        $sum: "$totalStudent"
      }
    }
  }
])
请参见此处的工作示例: