【问题标题】:Mongodb aggregate collectionMongoDB聚合集合
【发布时间】:2014-09-05 21:33:39
【问题描述】:

我在 mongodb 中学习聚合。我正在处理该系列:

                    {
                            "body" : ""
     ,
                    "email" : "oJJFLCfA@qqlBNdpY.com",
                    "author" : "Linnie Weigel"
            },
            {
                    "body" : ""
     ,
                    "email" : "ptHfegMX@WgxhlEeV.com",
                    "author" : "Dinah Sauve"
            },
            {
                    "body" : ""
    ,
                    "email" : "kfPmikkG@SBxfJifD.com",
                    "author" : "Zachary Langlais"
            }
            {
                    "body" : ""
     ,
                    "email" : "gqEMQEYg@iiBqZCez.com",
                    "author" : "Jesusa Rickenbacker"
            }
    ]

我尝试获取每个作者的正文数量。但是当我执行命令 sum of aggregate mongodb 时,结果是 1(因为结构只有一个元素)。我该怎么做这个操作?我尝试使用 $addToSet。但是我不知道如何获取集合的每个元素并进行操作。

【问题讨论】:

  • 能否请您附上您使用的代码?这也是你的 mongodb 大学作业吗?
  • 你说“执行命令总和”——没有这样的命令。您是在谈论 $group 阶段,您可以在其中使用 $sum 运算符来获得答案(只要您在正确的字段上分组)。
  • 我的代码是 var group = {$group:{_id:"$author",total:{$addToSet:"$cmets.author"}}}:
  • var group1 = {$group:{id:"$total",count:{$sum:1}}};db.posts.aggregate([group,group1})
  • 结果是作者的名字和计数1。这是错误的。我想找到作者和尸体的数量。我怎样才能获得和排列所有作者,然后对身体进行计数

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

为了计算每个作者的 cmets,您希望 $group 该作者和 $sum 出现次数。基本上只是一个 "$sum: 1" 操作。但似乎您将“cmets”作为一个数组,基于您自己的 cmets 和部分数据列表中的右括号。为此,您需要先使用$unwind 处理:

db.collection.aggregate([
   { "$unwind": "$comments" },
   { "$group": {
       "_id": "$comments.author",
       "count": { "$sum": 1 }
   }}
])

这将获得整个集合的作者所有作者 cmets 的总数。如果您刚刚获得每个文档的作者(或看起来像博客文章模型)的总 cmets,那么您将文档 _id 用作组声明的一部分:

db.collection.aggregate([
   { "$unwind": "$comments" },
   { "$group": {
       "_id": {
           "_id": "$_id"
           "author": "$comments.author"
       },
       "count": { "$sum": 1 }
   }}
])

然后,如果您想要每个文档的作者计数摘要,并且只返回一个文档以及数组中的所有作者,那么请使用此处的 $addToSet 以及另一个 $group 管道阶段:

db.collection.aggregate([
   { "$unwind": "$comments" },
   { "$group": {
       "_id": {
           "_id": "$_id"
           "author": "$comments.author"
       },
       "count": { "$sum": 1 }
   }},
   { "$group": {
       "_id": "$_id._id",
       "comments": {
           "$addToSet": {
               "author": "$_id.author",
               "count": "$count"
           }
       }
   }}
])

但实际上,作者值已经是唯一的,并且“集合”没有以任何方式排序,因此您可以在首次引入 $sort 后使用 $push 更改此设置,以使列表按生成的 cmets 数量排序:

db.collection.aggregate([
   { "$unwind": "$comments" },
   { "$group": {
       "_id": {
           "_id": "$_id"
           "author": "$comments.author"
       },
       "count": { "$sum": 1 }
   }},
   { "$sort": { "_id._id": 1, "count": -1 } },
   { "$group": {
       "_id": "$_id._id",
       "comments": {
           "$push": {
               "author": "$_id.author",
               "count": "$count"
           }
       }
   }}
])

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2017-07-13
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多