Home > Net >  MongoDB - Find duplicated values
MongoDB - Find duplicated values

Time:01-29

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.

Review document

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:

  1. Match the user you need to search for duplicates. ( It is best to create index on that field if your database is big )
  2. Group based on user and tour to identify how many reviews user provided in same tour.
  3. Filter only those where user has given more then one review
  4. Project the final fields more human readable

You can test here: playground

  •  Tags:  
  • Related