I'm making an application with MongoDB and C#. I use the MongoDB C# driver. I have the following collection:
{
_id: 5099803df3f4948bd2f98391,
title: Toy Story,
genres: [ "Animation", "Comedy" ]
},
{
_id: 5099803df3f4948bd2f98392,
title: Minions,
genres: [ "Animation", "Comedy", "Action" ]
}
Now I want to query on the data and get the how many movies there are for each genre. So the result should be:
Animation: 2
Comedy: 2
Action: 1
I'm trying to achieve this with this code.
database.GetCollection<Movie>("Movies")
.Aggregate()
.Unwind<Movie>(x => x.Genres)
.Group(x=>x.Genres, g => new
{
Genre= g.Key,
Count = g.Select(x=>x.Genres).Count()
}).ToList();
I changed it multiple times without success