$project with accumulators is returning many rows

Hello,

Please there is something I don’t understand.
In the second video of chapter 3, there is that code

db.icecream_data.aggregate([ { $project: { _id: 0, average_cpi: { $avg: "$trends.icecream_cpi" } } } ])

whis is returning exactly one row.

I tried to do the same thing but with the movies db. I don’t know why but it’s returning many rows.

db.movies.aggregate([{ $project: { _id: 0, highest_rating: { $max: "$num_mflix_comments" } } } ])

I don’t understand why.
Could someone give me an explanation please ?

Hey @mael-fosso

Which database and collections are you using for the above query? I cannot find a collection with num_mflix_comments as a field

Hi @mael-fosso

Try the following:

  1. Connect to M121 course Atlas Cluster, using the mongo shell (link was provided in Chapter 0, second lecture)
    mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
  2. Enter this command db.icecream_data.find().count(), and you will see that it contains only 1 document
  3. Then try this command db.movies.find().count(), you’ll get 44497 documents.

In your case, you are piping 44497 docs through the $project stage, and that is the same number you are going to get on output.

P.S. Are you sure documents in the movies collection have a field named num_mflix_comments?

1 Like

@mael-fosso, I think you are looking for $group rather than project.

1 Like

@mael-fosso

I believe that you’re in the wrong database. The database for this course is “aggregations” and you will automatically be in that database if you use the supplied connection string. The “mflix” database is used in a different course.