For the given structure I need to find out the count of the likes array based on the unique slug value
{
"_id" : ObjectId("4e8ae86d08101908e1000001"),
"name" : ["Name"],
"likes" : ["emp1"],
"slug": 'slugabcd'
}
{
"_id" : ObjectId("4e8ae86d08101908e1000002"),
"name" : ["Another ", "Name"],
"likes" : ["emp1","emp2","emp4"],
"slug": 'slugxyz'
}
{
"_id" : ObjectId("4e8ae86d08101908e1000002"),
"name" : ["Another ", "Name"],
"slug": 'slugpqr'
}
Here is my code but not working
db.blog.aggregate({slug:"slugxyz"},{$project:{NumberOfItemsInArray:{$size:"likes"}}}).count();
How can we achieve this?
likes