MongoDB.Driver.Builders如何分组并获得平均值

编程入门 行业动态 更新时间:2024-10-11 17:21:12
本文介绍了MongoDB.Driver.Builders如何分组并获得平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在使用C#MongoDB驱动程序,并且我们希望对时间戳的Date部分进行分组,并获取该日期的平均值.问题在于,我们无法使用构建器为该组找到正确的合奏.

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 correct synthax for the group using the builders.

此代码显示了如何使用BSON文档进行分组,但是我们发现synthax难以阅读且非常混乱!因此,我们正在寻找正确的合成器Synthax.

我们想使用Builders,因为它在C#中的类型更多,然后在管道中使用带有BsonDocuments的方法.这是一个代码段,前三个操作可以正常工作,但是我们找不到GroupBy.

We would like to use the Builders because it is more typed in C# then using the method with BsonDocuments in a pipeline. Here is a code snippet, where the first 3 operations work, but we can't find out GroupBy.

DateTime from = new DateTime(2014, 12, 2); DateTime to = new DateTime(2014, 12, 4); var id = "37d163c0-44cc-4907-94cf-1e26b5eec911"; var grp = new BsonDocument { { //Sort the documents into groups "$group", new BsonDocument { //Make the unique identifier for the group a BSON element consisting // of a field named Car. // Set its value to that of the Cars field // The Cars field is nolonger an array because it has now been unwound //{ "_id", new BsonDocument { { "Date", "$Date" } } }, { "_id",new BsonDocument{ new BsonDocument("year",new BsonDocument ("$year","$Date")), new BsonDocument("month",new BsonDocument ("$month","$Date")), new BsonDocument("day",new BsonDocument ("$dayOfMonth","$Date")) } }, { //Add a field named Owners "avgAmount", new BsonDocument { { "$avg" ,"$Value"} } } } } }; AggregateArgs aggregateArgs = new AggregateArgs() { Pipeline = new[] { new BsonDocument("$match", Query<Reading>.EQ(c => c.SensorId, id).ToBsonDocument()) , new BsonDocument("$match", Query<Reading>.LTE(c => c.Date, to).ToBsonDocument()) , new BsonDocument("$match", Query<Reading>.GTE(c => c.Date, from).ToBsonDocument()) , grp //, new BsonDocument("$group",GroupBy<Reading>.Keys(c=> c.Date).ToBsonDocument()) } }; IEnumerable<BsonDocument> documents = collection.Aggregate(aggregateArgs);

感谢所有帮助,我们已经在论坛上搜索了类似问题,但找不到正确的工作解决方案,问题1 或问题2 .

All help is appreciated, we already looked in simular questions on the forum, but can't find a correct working solution, question 1 or question 2.

推荐答案

带有新的MongoDB .NET驱动程序(2.0- docs.mongodb/ecosystem/drivers/csharp/),Linq支持得到了充分支持,这是新驱动程序使用的问题的句法.然后,在使用BsonDocument Synthax之前,.NET代码要可读得多.

with the new MongoDB .NET driver (2.0 - docs.mongodb/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; }

更多推荐

MongoDB.Driver.Builders如何分组并获得平均值

本文发布于:2023-10-23 14:21:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   MongoDB   Driver   Builders

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!