Aggregate Pipeline Help

Hello,

this is unrelated to the course content but I need help on a question, I am yet to go through the content in the course but need an answer to this question as soon as possible

I have a database with over 1000 reviews of products, I need to find all of the people with at least 3 reviews, and I need to show their reviewer ID, reviewer name, and the number of reviews they’ve written

I understand that I have to use an aggregate method but I am very confused on how to do it, so any help would be appreciated

Part of the solution looks like


...aggregate(
[
    {
        '$group' :
        {
            '_id' : "$_reviewerID" ,
           'count' : { '$sum' : 1 } ,
          'name' : '$_reviewerName'
        }
    }
   {
       $match : { 'count' : { $gte : 3 } }
   }
] )

I wrote that without testing or syntax checking. So you will have to correct a few mistakes. However, I want to add that people are here to learn and to help others to learn in the context of the course. We are not here to do your job work.

1 Like

1K reviews… sounds like reviews for a Small Business. Yours or a friend’s?

Tidied up and tested:

db.collectionName.aggregate([
   {
      $group: {
         _id: {
            '_reviewerID': '$_reviewerID', 
            '_reviewerName': '$_reviewerName'
         },
         '_reviewCount': {$sum: 1}
      }
   },
   {
      $match: {
         '_reviewCount': {$gte: 3}
      }
   }
]).pretty()

… replace collectionName with the name of the collection and ensure that you use the correct db.

I am curious as to why you included reviewerName in the _id. I assumed the ID was unique. I assume the name could change with the mood of the review. Sometimes Alinna can’t stop eating but at other time well she might be overfed. For example, if she changes her profile from one review to the other. The best would be to do a $lookup in the profile collection to get the current “mood less” name but we do not know if there is such a collection.

1 Like

Thanks everyone for the help, I understand we are here to learn and I’m going to start the course in the morning

Yes @steevej-1495, that was my assumption too and because @Litz29 wanted the name in there I wanted to keep it simple without utilising $lookup… with 1K reviews I wouldn’t imagine there being many reviewers with 3+ reviews and there shouldn’t be too many name discrepancies that can’t be eye-balled. Now left with @Litz29 to optimise.

1 Like

Does not work but appreciate the help, I think I need to use the $group, $match, and $project for this question

I’m sure that you can do better than “does not work”. The code provided is tested and is a very basic example of the aggregation pipeline. So based on your simple requirements, we would have advised if there was a better way.

It’s either:

  1. You didn’t implement it correctly, or
  2. Your requirements have changed

Which one is it? Explain with screenshots too, please.

1 Like

The code supplied makes use of $group and $match already. The $project stage is optional and superfluous since the only fields present after the $group are the one you wanted.

I understand, when I implemented the code I only received two reviewers names, ID and review count back, but when looking over the database there were more than 2 people who left 3 or more reviews

And if I may add what was supplied here is on line with what was supplied in https://www.mongodb.com/community/forums//t/aggregate-pipeline-method-help/810/5?u=steevej

1 Like

May be the reviewerName changes between reviews so you could try by not including it in the _id.

It sounds more like reviewerID does not uniquely identify a reviewer, in which you want to remove reviewerID.

We need to see proof.