Akis
Akis

Reputation: 143

Group by in mongoDB gives strange result

I have multiple documents like this below:

{
    "IMDBURL":"http:\/\/us.imdb.com\/M\/title-      exact?Schrei%20aus%20Stein%20(1991)",
    "release_date":"08-Mar-1996",
    "ratings":[{"user_id":916,"rating":3,"timestamp":"880845755"}], 
    "genre":["Drama","Thriller"],
    "video":"","title":"Scream of Stone (Schrei aus Stein) (1991)"
}

and I trying to show the titles of the movies with the number of genres they belong.

So far I am doing this

db.bigdata.aggregate(
    {$unwind:"$genre"},
    {$group: {_id:"$title",genres:  {$sum:"$genre"}}}
)

I receive this response

 {"_id" : "Sunchaser, The (1996)", "genres" : 0 }
 { "_id" : "Kika (1993)", "genres" : 0 }

Can someone please tell me what I am doing wrong?

Upvotes: 3

Views: 52

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

From mongodb $sum documentation:

{ $sum : <field> }  Numeric                     Sum of Values
{ $sum : <field> }  Numeric and Non-Numeric     Sum of Numeric Values
{ $sum : <field> }  Non-Numeric or Non-Existent 0

As you can see, third case clearly states - sum of non-numeric fields will give you 0 as result. If you want to get collection of all genres back, then you should use $addToSet. If you want to get count of genres, you should use

{$group: {_id:"$title", genres: {$sum:1}}}

And there is no need to unwind and group for that (unless you have duplicated genres) - simple $size will do the same in projection operator.

Upvotes: 2

Related Questions