M001: Mongodb basics, Chapter 5: Indexing and Aggregation Pipeline Lab: Aggregation Framework

I get this error message(“The field ‘count’ must be an accumulator object”, ), on lab 1, and I still cannot see what is wrong with the code. Please kindly help…!!

MongoDB Enterprise atlas-o1jdjg-shard-0:PRIMARY> db.listingsAndReviews.aggregate([ {"$project": {"room_type":1, "_id":0}}, {"$group": {"_id": "$room_type"}} ]).pretty()
MongoDB Enterprise atlas-o1jdjg-shard-0:PRIMARY> 
MongoDB Enterprise atlas-o1jdjg-shard-0:PRIMARY> 
MongoDB Enterprise atlas-o1jdjg-shard-0:PRIMARY> **db.listingsAndReviews.aggregate([**
**... {"$project": {"room_type":1, "_id":0}},**
**... {"$group": {"_id": "room_type", "count":{"sum":1}}}**
**... ]).pretty()**
2022-01-24T05:15:01.353+0000 E QUERY    [js] Error: command failed: {
        "operationTime" : Timestamp(1643001301, 7),
        "ok" : 0,
        "errmsg" : "The field 'count' must be an accumulator object",
        "code" : 40234,
        "codeName" : "Location40234",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1643001301, 7),
                "signature" : {
                        "hash" : BinData(0,"PJNZrRUuC/BKQjlFv6tNRK5f5iw="),
                        "keyId" : NumberLong("6990363765946449922")
} : aggregate failed :
MongoDB Enterprise atlas-o1jdjg-shard-0:PRIMARY>

You have to use one of the accumulator rather that sum.

I have a similar problem, but I am not doing the count. I was just trying to find what room types are included in the airbnb database. I did this:
db.listingsAndReviews.aggregate( [{$project:{“room_type”:1, _id:0}}, {$group:{"_id" : “room_type”}}]).

I got no results. I did it exactly as the example during the lecture. What am I missing? Thanks to whoever reads these things.


and look for examples to see what is wrong with the <expression> part in

The aggregation page doesn’t explain that an expression format should be different than what I have-- it mostly provides a list of the possible aggregate functions. The example used in the lesson shows the expression as “address.country.” But since in my task the field I want is a top level field, I do not need a period between the top level and sub-level. Otherwise, my script is identical. Also, the lesson page has a section that says // Group By Expression (which I assume is an instruction note and not to be part of the query). I tried to write it to match the format : { : }, like this: …{"_id":{ “$mergeObject”:“room_type”}}, and that returned an error. Same problem when I write {"_id":{“room_type”:{}}}. Can you direct me to the specific lesson? Thanks for your help.

Are you saying all I needed was to put the $ sign in front of the field? Like this:
db.listingsAndReviews.aggregate( [ {"$project":{“room_type”:1, “_id”:0}},{"$group":{"_id":"$room_type"}}])? I hope so. That sucks and was such a small item to overlook. It would have been nice if you just reminded me that I want to get the value of the field, which requires a slightly different format.

So I think I got it. Thanks again for your help.

1 Like

The extra reading and thinking you did to figure out by yourself is worth more than any direct straight answer.

Give a man a fish,
you feed him for the day

Teach a man to fish,
you feed him for the rest of his life.