I am fairly new to MongoDB, and here goes my question.
I have a collection containing reviews where each document contains a tour id, a user id, and the review itself. How can I find the tour(s) in which a user has posted more than one review on that tour? (I have attached a snapshot of a review document)
I believe the solution lies in the aggregation but I can't figure out how to achieve it.
CodePudding user response:
db.collection.aggregate([
{
$match: {
user: "John"
}
},
{
$group: {
_id: {
u: "$user",
t: "$tour"
},
cnt: {
$sum: 1
}
}
},
{
$match: {
cnt: {
$gt: 1
}
}
},
{
$project: {
_id: 0,
user: "$_id.u",
tour: "$_id.t",
number_of_reviews: "$cnt"
}
}
])
explained:
- Match the user you need to search for duplicates. ( It is best to create index on that field if your database is big )
- Group based on user and tour to identify how many reviews user provided in same tour.
- Filter only those where user has given more then one review
- Project the final fields more human readable
You can test here: playground
