I have a book list in json as following:
{
"_id" : ObjectId("1"),
"author" : [
"Mary",
"Tony"
],
"booktitle" : "Book1",
"Category" : "Children"
}
{
"_id" : ObjectId("2"),
"author" : [
"Joe",
"Tony"
],
"booktitle" : "Book2",
"Category" : "Children"
}
{
"_id" : ObjectId("3"),
"author" : [
"Joe",
],
"booktitle" : "Book3",
"Category" : "comedy"
}
.......
I hope to get top 10 authors who write books belongs to "Children". Because some book are not just written by one author. I don't know how to do it.
Given the example above, book1 and book2 belongs to "Children", Tony writes 2 books, Mary and Joe writes 1 book. Thus, Top 3 writers are in order Tony, Mary, Joe.
I just write down:
db.table.find({Category: "Children"}).aggregate({$group:{_id: '', count : {$sum : 1}}}, {$sort: {count : -1}})
but don't know how to write the group part. Thanks.