【问题标题】:How to take the array count based on contentId如何根据 contentId 获取数组计数
【发布时间】:2020-07-14 23:24:19
【问题描述】:

我有以下 mongodb 结构

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

},
{
    "userId" : "5d3014fe56db690a5959a874",
    "contentId" : "2",
    "userAnswer" : {
    
        "uploads" : [
            null
        ]
    }
}

contentId 有 (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

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    [ null ]不是一个空数组,它是一个包含1个元素的数组,所以它的大小确实是1。

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

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

    【讨论】:

      【解决方案2】:

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

      我已经使用$filter 运算符来投影没有null 值的userAnswer.uploads 数组,然后可以使用$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"
            }
          }
        }
      ])
      

      在此处查看工作示例:Mongo Playground

      【讨论】:

        猜你喜欢
        • 2019-11-22
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 1970-01-01
        相关资源
        最近更新 更多