I have documents with subdocuments where the field "stars" in the subdocuments have either "1" or "0". I want to count them at the document level based on those values in the field "stars".
This is how the current document looks like:
{
"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
}
And this is the expected output:
{
"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
}
By adding two new fields: "positive_reviews" if {"reviews.stars":1} and "negative_reviews" if {"reviews.stars":0} with the count values
Try as below:
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"}
}
}
])
Result Response:
/* 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)
}