【问题标题】:MongoDb aggregate the count/sum of 3 fieldsMongoDb 聚合 3 个字段的计数/总和
【发布时间】:2017-08-16 17:16:08
【问题描述】:

我试图获取用户喜欢、分享或评论的内容的总和。

我的内容模型具有以下结构。

Content = {
  _id: ObjectId(),
  author: ObjectId(),
  value: <string of post content>
  tags: [<trings>],
  likes: [array of user ids],
  shares: [array of user ids],
  comments: [{
     ContentId( of the same schema ),
     User Id
  }]
}

现在,id 喜欢聚合内容文档, 并得到这样的结果。

{
   likes: 20,
   shares: 10,
   comments: 5
}

所以,简而言之,只要有内容, 其中用户 id 在 likes 数组中, likes 增加 1。

共享和 cmets 也是如此。

我不确定聚合框架是否可以做到这一点。 我认为不是,但也许一些 mongodb 大师更了解

快速编辑。部分基于提交的第一篇文章,我做了这个。 现在,它似乎工作了,但我确定有某种陷阱,我错过了,因为它看起来太简单了:)

db.getCollection('contents').aggregate( [


{$facet: {
      "likes": [
         {$match: {likes: ObjectId("596537d6b63edc2318ee9f0c")} },
         {$group: {
           _id : "$likes",
           count: { $sum: 1 }
         }},
         { $project: { count: 1} }
      ],
      "shares": [
         {$match: {shares: ObjectId("596537d6b63edc2318ee9f0c")} },
         {$group: {
           _id : "$shares",
           count: { $sum: 1 }
         }},
         { $project: { count: 1} }
      ],
      "posts": [
         {$match: {$and: [
             {user: ObjectId("596537d6b63edc2318ee9f0c")},
             {parent: {$exists: false} }
         ]} },
         {$group: {
           _id : "$_id",
           count: { $sum: 1 }
         }},
         { $project: { count: 1} }
      ]
  }
}]);

你能发现上面的代码有什么问题吗?

【问题讨论】:

    标签: mongodb aggregation-framework nosql


    【解决方案1】:

    您可以在 3.4 版本中尝试以下聚合。

    下面的查询将$group所有文档并使用$in运算符检查输入user_id是否在数组中。

    对于likesshares 数组,如果找到user_id,则使用1,否则设置0$sum 以聚合所有文档。

    对于comments,这是一个两步过程,因为它是数组数组。

    $grouping 步骤检查内容文档中的 user_id,检查每个元素中的输入 user_id,如果匹配则输出 1,否则输出 0

    comments 在小组赛结束后将有数组值数组[[1, 0], [1], [1]]。下一步是使用 $reduce 运算符对所有值求和以获得 comments 计数。

    db.collection_name.aggregate([
      {
        "$group": {
          "_id": null,
          "likes": {
            "$sum": {
              "$cond": [
                {
                  "$in": [
                    user_id,
                    "$likes"
                  ]
                },
                1,
                0
              ]
            }
          },
          "shares": {
            "$sum": {
              "$cond": [
                {
                  "$in": [
                    user_id,
                    "$shares"
                  ]
                },
                1,
                0
              ]
            }
          },
          "comments": {
            "$push": {
              "$map": {
                "input": "$comments",
                "as": "comment",
                "in": {
                  "$cond": [
                    {
                      "$in": [
                        user_id,
                        "$$comment.user_id"
                      ]
                    },
                    1,
                    0
                  ]
                }
              }
            }
          }
        }
      },
      {
        "$addFields": {
          "comments": {
            "$reduce": {
              "input": "$comments",
              "initialValue": 0,
              "in": {
                "$add": [
                  "$$value",
                  {
                    "$sum": "$$this"
                  }
                ]
              }
            }
          }
        }
      }
    ])
    

    【讨论】:

      【解决方案2】:
      db.collection.aggregate( [
        {
          $facet: {
            "LikeCategory": [
              { $unwind: "$likes" },
              { $group : {
                 _id : "$likes",
                 count: { $sum: 1 }
               }
              },{ $project : {
                userId : "$_id"
                 _id : 0,
                  count : 1
      
              }}
            ],
            "ShareCategory": [
              { $unwind: "$shares" },
              { $group : {
                 _id : "$shares",
                 count: { $sum: 1 }
               }
              },{ $project : {
                userId : "$_id"
                 _id : 0,
                  count : 1
      
              }}
            ],
            "CommentCategory": [
              { $unwind: "$comments" },
              { $group : {
                 _id : "$comments.userId",
                 count: { $sum: 1 }
               }
              },{ $project : {
                userId : "$_id"
                 _id : 0,
                  count : 1
      
              }}
            ]
      }
      }
      ];
      

      通过这种方式,您将能够找出计数。上面的代码可能有一些语法问题,但我希望你能解决你的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 2015-11-22
        • 2014-02-27
        • 1970-01-01
        • 1970-01-01
        • 2019-04-06
        相关资源
        最近更新 更多