【问题标题】:Mongodb Document : Is there any way to count sub-documents with a condition?Mongodb Document : 有没有办法计算有条件的子文档?
【发布时间】: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} 带有计数值

【问题讨论】:

    标签: mongodb subdocument


    【解决方案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)
    }
    

    【讨论】:

    • 这正是我想要的!非常感谢您的及时回复!
    猜你喜欢
    • 2020-07-25
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多