问题
I have a database of over 2000 reviews for products on an online store, and I am trying to find all of the users who have written minimum 5 reviews, and I would like to list their names, number of reviews, and reviewer ID
I understand that I have to use aggregate method to do this but when trying I have no luck as I am still new to mongodb
I have added a picture to show an example of some of the data in the database
An example of one entry into the database is as below;
{
"_id": ObjectId("5d0b70f2d7367de7f5fa0ee1"),
"SH_reviewerID": "A2G0LNLN79Q6HR",
"SH_asin": "0000031887",
"SH_reviewerName": "aj_18 \"Aj_18\"",
"SH_helpful": [
1,
1
],
"SH_reviewText": "This was a really good",
"SH_overall": 4.0,
"SH_summary": "Really Cute but rather short.",
"SH_unixReviewTime": 1337990400,
"SH_reviewTime": "05 26, 2012"
}
I have tried to work it out for over several days but have no luck, I think we need to use $match, $group, $project
回答1:
I guess you need to run this aggregation:
db.review.aggregate([
{
$group: {
_id: "$SH_reviewerID",
name: {
$first: "$SH_reviewerName"
},
number: {
$sum: 1
}
}
},
{
$match: {
number: {
$gte: 5
}
}
},
//Optional - Order views descendant
{
$sort: {
number: -1
}
},
//Optional - Change field names
{
$project: {
_id: 0,
SH_reviewerID: "$_id",
SH_reviewerName: "$name",
SH_reviewerViews: "$number"
}
}
])
MongoPlayground
来源:https://stackoverflow.com/questions/60327604/is-there-a-way-to-use-the-aggregate-pipeline-method-for-this-query