public MostResponse GetMostUsedAggregation() { var query = _books.Aggregate() .Unwind <Book, UnwindBook>(a => a.ListOfTags) .Group(e => e.ListOfTags, g => new { Tag = g.Key, Count = g.Count() }) .Sort("{Count: -1}"); var doc = query.First(); var mostUsed = new MostResponse { Tag = doc.Tag, Count = doc.Count }; return(mostUsed); }
public MostResponse GetMostUsed() { //var books = _books.Find(book => true).ToList(); var allTags = _books.AsQueryable().SelectMany(a => a.ListOfTags); var query = allTags.GroupBy(x => x.ToString()) .Select(group => new { Tags = group.Key, Count = group.Count() }) .OrderByDescending(x => x.Count); //var query = _books.AsQueryable().GroupBy(x => x.ListOfTags) //.Select(group => new { Tags = group.Key, Count = group.Count() }) //.OrderByDescending(x => x.Count); var item = query.First(); var mostUsed = new MostResponse { Tag = item.Tags, Count = item.Count }; return(mostUsed); }