Mongodb Document : 有什么方法可以计算有条件的子文档吗?

Mongodb Document : Is there any way to count sub-documents with a condition?

我有包含子文档的文档,其中子文档中的字段“stars”有“1”或“[=21=” ]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” if {"reviews.stars":1} 和“ negative_reviews" 如果 {"reviews.stars":0} 与计数值

尝试如下:

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)
}