I know that there are many topics talking about this, but I can’t understand what went wrong in my implementation.
I have the following documents:
{ "_id" : ObjectId("510a353929e16756d5000009"), "skills" : [ [ "UserEmbed::Author::Skills::Copywriting", "UserEmbed::Author::Skills::Proofreading", "UserEmbed::Author::Skills::Proofreading", "UserEmbed::Author::Skills::Translation", "UserEmbed::Author::Skills::Translation", "UserEmbed::Author::Skills::Translation" ] ] }
I would like something like this:
{ "UserEmbed::Author::Skills::Copywriting": 1, "UserEmbed::Author::Skills::Proofreading": 2, "UserEmbed::Author::Skills::Translation": 3 }
Here's what I have (the first $group should get the document above from my original document structure):
aggregate([ { $group: { _id: "$_id", skills: { $addToSet : "$author_profile.skills._type" } } }, { $unwind : "$skills" }, { $group : { _id : "$skills", count: { $sum : 1 } } }])
Which returns something like this (with other documents):
{ "_id" : [ "UserEmbed::Author::Skills::Copywriting", "UserEmbed::Author::Skills::Copywriting", "UserEmbed::Author::Skills::Copywriting", "UserEmbed::Author::Skills::Translation", "UserEmbed::Author::Skills::Translation", "UserEmbed::Author::Skills::Translation" ], "count" : 1 }
$group seems to be working incorrectly. I didn’t understand something?