Пример #1
0
        public async Task <List <GroupResult> > AggregateCountAsync(string author, GroupTimeRange groupTimeRange, FilterSettings filterSettings)
        {
            /*
             * { "$match" : { "Author" : "test" } },
             * { "$group" : { "_id" : { "$dateToString" : { "format" : "%Y-%m-%d", "date" : "$Created", "timezone" : "Europe/Berlin" } }, "count" : { "$sum" : 1 } } },
             * { "$project" : { "_id" : 0, "Key" : "$_id", "Value" : "$count" } },
             * { "$sort" : { "Key" : 1 } }
             */

            var projectStage = new BsonDocument
            {
                { "_id", 0 },
                { "Key", "$_id" },
                { "Value", "$count" }
            };

            var watch           = Stopwatch.StartNew();
            var aggregateResult = this.documentCollection.Aggregate()
                                  .Match(this.BuildMatchStage(author, filterSettings))
                                  .Group(MongoOp.GroupByDate("$Created", groupTimeRange, MongoOp.Count("count")))
                                  .Project(projectStage)
                                  .Sort(MongoOp.SortBy("Key", asc: true));

            var bsonResult = await aggregateResult.ToListAsync();

            var result = bsonResult.Select(doc => BsonSerializer.Deserialize <GroupResult>(doc))
                         .ToList();

            watch.Stop();

            if (this.logger.IsEnabled(LogLevel.Debug))
            {
                this.logger.LogDebug("Explore took {ms}ms.", watch.ElapsedMilliseconds);
            }

            return(result);
        }