Mongodb Document : 有没有办法计算有条件的子文档?

Posted

技术标签:

【中文标题】Mongodb Document : 有没有办法计算有条件的子文档?【英文标题】:Mongodb Document : Is there any way to count sub-documents with a condition? 【发布时间】:2019-09-03 05:52:30 【问题描述】:

我有包含子文档的文档,其中子文档中的“stars”字段具有“1”或“0”。我想根据“stars”字段中的值在文档级别对它们进行计数。

这是当前文档的样子:


  "name": "Hotel A",
  "category": "hotel",
  "reviews": [
    
      "title": "A",
      "stars": 1
    ,
    
      "title": "B",
      "stars": 1
    ,
    
      "title": "C",
      "stars": 0
    
  ],
  "total_reviews": 3
,
  "name": "Hotel B",
  "category": "hotel",
  "reviews": [
    
      "title": "A",
      "stars": 1
    ,
    
      "title": "B",
      "stars": 1
    ,
    
      "title": "C",
      "stars": 0
    ,
        
      "title": "D",
      "stars": 0
    ,
    
      "title": "E",
      "stars": 1
    ,
    
      "title": "F",
      "stars": 0
    
  ],
"total_reviews": 6

这是预期的输出:


  "name": "Hotel A",
  "category": "hotel",
  "reviews": [
    
      "title": "A",
      "stars": 1
    ,
    
      "title": "B",
      "stars": 1
    ,
    
      "title": "C",
      "stars": 0
    
  ],
  "positive_reviews": 2,
  "negative_reviews": 1,
  "total_reviews": 3
,
  "name": "Hotel B",
  "category": "hotel",
  "reviews": [
    
      "title": "A",
      "stars": 1
    ,
    
      "title": "B",
      "stars": 1
    ,
    
      "title": "C",
      "stars": 0
    ,
        
      "title": "D",
      "stars": 0
    ,
    
      "title": "E",
      "stars": 1
    ,
    
      "title": "F",
      "stars": 0
    
  ],
"positive_reviews": 3,
"negative_reviews": 3,
"total_reviews": 6

通过添加两个新字段:“positive_reviews”如果 "reviews.stars":1 和“negative_reviews”如果 >"reviews.stars":0 带有计数值

【问题讨论】:

【参考方案1】:

尝试如下:

db.collection.aggregate([
    
        $project: 
            "_id" : 1,
            "name" : 1,
            "category" : 1,
            "reviews" : 1,
            "positive_reviews":
            
               $reduce: 
                 
                   input: "$reviews",
                   initialValue: 0,
                   in:  
                       $add: ["$$value",  $cond:[ $eq: ["$$this.stars", 1] , 1, 0 ]  ] 
                   
                 
            ,
            "negative_reviews":
            
               $reduce: 
                 
                   input: "$reviews",
                   initialValue: 0,
                   in:  
                       $add: ["$$value",  $cond:[ $eq: ["$$this.stars", 0] , 1, 0 ]  ] 
                   
                 
            ,
            "total_reviews":  $size: "$reviews"
        
    
])

结果响应:

/* 1 createdAt:12/04/2019, 18:24:50*/

    "_id" : ObjectId("5cb08a9a952e3a179190d996"),
    "name" : "Hotel A",
    "category" : "hotel",
    "reviews" : [
        
            "title" : "A",
            "stars" : 1
        ,
        
            "title" : "B",
            "stars" : 1
        ,
        
            "title" : "C",
            "stars" : 0
        
    ],
    "positive_reviews" : 2,
    "negative_reviews" : 1,
    "total_reviews" : NumberInt(3)
,

/* 2 createdAt:12/04/2019, 18:24:50*/

    "_id" : ObjectId("5cb08a9a952e3a179190d997"),
    "name" : "Hotel B",
    "category" : "hotel",
    "reviews" : [
        
            "title" : "A",
            "stars" : 1
        ,
        
            "title" : "B",
            "stars" : 1
        ,
        
            "title" : "C",
            "stars" : 0
        ,
        
            "title" : "D",
            "stars" : 0
        ,
        
            "title" : "E",
            "stars" : 1
        ,
        
            "title" : "F",
            "stars" : 0
        
    ],
    "positive_reviews" : 3,
    "negative_reviews" : 3,
    "total_reviews" : NumberInt(6)

【讨论】:

这正是我想要的!非常感谢您的及时回复!

以上是关于Mongodb Document : 有没有办法计算有条件的子文档?的主要内容,如果未能解决你的问题,请参考以下文章

在 MongoDB 中设计追随者和追随者模式?

在php中统计mongodb

mongodb Failed to start LSB: An object/document-oriented dat

请大神指导从大日志文件中统计关键字次数的办法

有没有办法使用persistent-mongoDB中的低级mongoDB后端?

有没有办法防止 MongoDB 向集合名称添加复数形式?