【问题标题】:MongoDB how to get count of comments + repliesMongoDB如何获得评论+回复的数量
【发布时间】:2020-06-23 06:28:53
【问题描述】:

所以我的 MongoDB 实例中有这个结构。

{
  "post_body": "test",
  "author": "test1",
  "comments":[
    {
      "comment_body": "comment test",
      "comment_author": "test2",
      "replies": [
        {
          "reply_body": "reply test1",
          "reply_author": "test3"
        },
        {
          "reply_body": "reply test2",
          "reply_author": "test2"
        }
      ]
    }
  ]
}

我想获得 cmets + 回复的总数。

所以我想要的输出应该是

{
    "post_body": "test"
    "author": "test1",
    "comment_count": 3
}

到目前为止,使用 $project 只返回 cmets 的总数。我想得到 cmets + 回复的总数

【问题讨论】:

    标签: python mongodb mongodb-query pymongo aws-documentdb


    【解决方案1】:

    使用Aggregation Pipeline我们可以得到想要的结果

    以下查询使用管道阶段$project$unwind 和管道运算符$size$sum

    db.collection_name.aggregate([
      { $project: {
         "post_body": 1, 
         "author": 1,
         "comments":1,
         "comments_size":{$size: "$comments" }
        }
      }, 
      { $unwind: "$comments" }, 
      { $project: {
         "post_body": 1, 
         "author": 1,
         "comments":1,
         "comments_size":1, 
         "replies_size" : {$size: "$comments.replies"} 
        }
      },
      { $project: {
         "_id":0, 
         "post_body": 1,
         "author": 1,
         "comments_count":{$sum:["$comments_size", "$replies_size"]}
        }
      }
    ])
    

    聚合查询的第一部分使用 $project,我们只是将所需的属性投影到下一阶段,并且我们正在查找 cmets 数组的大小。 cmets数组的大小存储在一个临时属性comments_size

    第二部分使用$unwind打破commentscomments.replies中的嵌套数组,comments数组被展开,comments.replies数组保持不变

    第三部分使用 $project 查找回复的大小,并将其存储在一个临时属性replies_size

    第四部分也是最后一部分再次使用 $project 和 comments_sizereplies_size 的 $sum 来获得所需的结果

    【讨论】:

      【解决方案2】:
      import pymongo
      from bson.objectid import ObjectId
      myclient = pymongo.MongoClient("mongodb://localhost:27017/")
      db = myclient["test"]
      comments_col = db["comments"]
      doc = {
          "post_body": "test",
          "author": "test1",
          "comments": [
              {
                  "comment_body": "comment test",
                  "comment_author": "test2",
                  "replies": [
                      {
                          "reply_body": "reply test1",
                          "reply_author": "test3"
                      },
                      {
                          "reply_body": "reply test2",
                          "reply_author": "test2"
                      },
                  ]
              }
          ]
      }
      
      def insert(doc1):
          id_comment = comments_col.insert(doc1)
          return id_comment
      def find(id_comment):
          comment = comments_col.find_one({"_id": ObjectId(str(id_comment))})
          return comment
      if __name__ == "__main__":
          id_comment = insert(doc)
          comment = find(id_comment)
          print("comments with replies : ", comment["comments"])
          print("\n")
          print("replies : ", comment["comments"][0]["replies"])
          print("\n")
          print("comment_author : ", comment["comments"][0]["comment_author"])
          print("\n")
          print("comment_body : ", comment["comments"][0]["comment_body"])
      

      【讨论】:

      • 最好使用聚合管道,这样它将对集合中的所有文档执行操作
      猜你喜欢
      • 2020-10-02
      • 2012-12-16
      • 1970-01-01
      • 2018-01-13
      • 2022-01-27
      • 2013-01-21
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多