MongoDB.Driver.Builders how to group and get average

前端 未结 1 1875
孤城傲影
孤城傲影 2020-12-31 18:23

We are using the C# MongoDB driver, and we would like to group on the Date part of a timestamp and get the average for that date. The problem is that we can\'t find the corr

相关标签:
1条回答
  • 2020-12-31 18:49

    with the new MongoDB .NET driver (2.0 - http://docs.mongodb.org/ecosystem/drivers/csharp/) , Linq support is fully suported, here is the synthax of the question usign the new driver. Much more readable .NET code then before using the BsonDocument synthax.

        public async Task<List<DailyStat>> GetLast31DaysReport(string id)
        {
            var mc = new MongoClient(_url);
            var db = mc.GetDatabase(DbName);
            var collection = db.GetCollection<Reading>(CollectionName);
    
            DateTime from = DateTime.Now.AddDays(-31);
            DateTime to = DateTime.Now;
    
            var output = await collection.Aggregate()
                .Match(r => r.SensorId == id)
                .Match(r => r.Date <= to)
                .Match(r => r.Date >= to.AddDays(-31))
                .Group(r => new { groupedYear = r.Date.Year, groupedMonth = r.Date.Month, groupedDay = r.Date.Day }, g =>
                    new {
                        Key = g.Key,
                        avgValue = g.Average(x => x.Value),
                        minValue = g.Min(x => x.Value),
                        maxValue = g.Max(x => x.Value)
                    })
                .Project(r => new DailyStat()
                    {
                        Day = r.Key.groupedDay,
                        Month = r.Key.groupedMonth,
                        Year = r.Key.groupedYear,
                        Value = r.avgValue,
                        MinValue = r.minValue,
                        MaxValue = r.maxValue
                    })
                .ToListAsync().ConfigureAwait(false);
    
            var returnList = new List<DailyStat>();
            while (returnList.Count < 31)
            {
                var value = output.FirstOrDefault(rec => rec.Day == from.Day && rec.Month == from.Month && rec.Year == from.Year);
                returnList.Add(value ?? new DailyStat() { Month = from.Month, Year = from.Year, Day = from.Day, Value = 0, MaxValue = 0, MinValue = 0 });
                from = from.AddDays(1);
            }
            return returnList;
        }
    
    0 讨论(0)
提交回复
热议问题