MongoDB 每月分组
MongoDB monthly grouping
我试着按月对值进行分组,然后得到每个月的平均值。
这里是我使用的聚合方法
var aggregateArgs = new AggregateArgs();
aggregateArgs.Pipeline =
new[]
{
new BsonDocument{{ "$match", new BsonDocument
{{ "_Timestamp", new BsonDocument {
{"$gte", _start}, // start date is 2016-08-04T23:15:00.000+01:00
{"$lt", _end}// end date is 2017-09-04T23:15:00.000+01:00
}}}
}},
new BsonDocument("$project",
new BsonDocument
{
{ "_id" , 0 },
{ "new_range", new BsonDocument {
{"year", new BsonDocument("$year", "$_Timestamp")},
{"month", new BsonDocument("$month", "$_Timestamp")},
}
}
}
),
new BsonDocument("$group",
new BsonDocument
{
{"_id", "$new_range" },
{"MonthlyAverage", new BsonDocument("$avg", "$totalfundspent")},
}),
};
但我明白了
"Enumeration yielded no results"
我做错了什么?我的 MongoDB 版本是 3.4.4
我的文件示例
{
"_Timestamp" : ISODate("2017-08-04T23:15:00.000+01:00"),
"totalfundspent" : 1138.0,
}
{
"_Timestamp" : ISODate("2017-08-05T23:15:00.000+01:00"),
"totalfundspent" : 638.0,
}
我认为问题在于日期未被解析为 ISODate("2016-08-04T23:15:00.000+01:00")
,将其转换为 DateTime 并将其传递给 BsonDocument 可确保发生这种情况。
var _start = Convert.ToDateTime("2016-08-04T23:15:00.000+01:00");
var _end = Convert.ToDateTime("2017-09-04T23:15:00.000+01:00");
var match = new BsonDocument {
{
"_Timestamp",
new BsonDocument {
{
"$gte", _start
}, {
"$lt", _end
}
}
}
};
var project = new BsonDocument {
{
"_id",
0
}, {
"new_range", new BsonDocument {
{
"year", new BsonDocument("$year", "$_Timestamp")
},
{
"month",
new BsonDocument("$month", "$_Timestamp")
},
}
}
};
var group = new BsonDocument {
{
"_id",
"$new_range"
}, {
"MonthlyAverage", new BsonDocument("$avg", "$totalfundspent")
}
};
var result = this.coll.Aggregate().Match(match).Project(project).Group(group).ToList();
如果你想调试查询,你可以在 .ToList()
之前调用 .ToString()
,就像这样
var jsonQuery = this.coll.Aggregate().Match(match).Project(project).Group(group).ToString();
这为您提供了一个 json 文档,您可以用它自己查询 Mongo。
我试着按月对值进行分组,然后得到每个月的平均值。
这里是我使用的聚合方法
var aggregateArgs = new AggregateArgs();
aggregateArgs.Pipeline =
new[]
{
new BsonDocument{{ "$match", new BsonDocument
{{ "_Timestamp", new BsonDocument {
{"$gte", _start}, // start date is 2016-08-04T23:15:00.000+01:00
{"$lt", _end}// end date is 2017-09-04T23:15:00.000+01:00
}}}
}},
new BsonDocument("$project",
new BsonDocument
{
{ "_id" , 0 },
{ "new_range", new BsonDocument {
{"year", new BsonDocument("$year", "$_Timestamp")},
{"month", new BsonDocument("$month", "$_Timestamp")},
}
}
}
),
new BsonDocument("$group",
new BsonDocument
{
{"_id", "$new_range" },
{"MonthlyAverage", new BsonDocument("$avg", "$totalfundspent")},
}),
};
但我明白了
"Enumeration yielded no results"
我做错了什么?我的 MongoDB 版本是 3.4.4
我的文件示例
{
"_Timestamp" : ISODate("2017-08-04T23:15:00.000+01:00"),
"totalfundspent" : 1138.0,
}
{
"_Timestamp" : ISODate("2017-08-05T23:15:00.000+01:00"),
"totalfundspent" : 638.0,
}
我认为问题在于日期未被解析为 ISODate("2016-08-04T23:15:00.000+01:00")
,将其转换为 DateTime 并将其传递给 BsonDocument 可确保发生这种情况。
var _start = Convert.ToDateTime("2016-08-04T23:15:00.000+01:00");
var _end = Convert.ToDateTime("2017-09-04T23:15:00.000+01:00");
var match = new BsonDocument {
{
"_Timestamp",
new BsonDocument {
{
"$gte", _start
}, {
"$lt", _end
}
}
}
};
var project = new BsonDocument {
{
"_id",
0
}, {
"new_range", new BsonDocument {
{
"year", new BsonDocument("$year", "$_Timestamp")
},
{
"month",
new BsonDocument("$month", "$_Timestamp")
},
}
}
};
var group = new BsonDocument {
{
"_id",
"$new_range"
}, {
"MonthlyAverage", new BsonDocument("$avg", "$totalfundspent")
}
};
var result = this.coll.Aggregate().Match(match).Project(project).Group(group).ToList();
如果你想调试查询,你可以在 .ToList()
之前调用 .ToString()
,就像这样
var jsonQuery = this.coll.Aggregate().Match(match).Project(project).Group(group).ToString();
这为您提供了一个 json 文档,您可以用它自己查询 Mongo。