我想做的就是按DateEntered和Role分组并获取计数。下面的代码可以工作,但是在完整日期的组到第二个。我需要按天分组(MM / DD / YYYY)
我也想使用Lambda表达式。
先谢谢你。
var group = new BsonDocument {
{ "_id", new BsonDocument
{
{ "DateEntered", "$DateEntered" },
{ "Role", "$Role" },
}
},
{ "Count", new BsonDocument("$sum", 1) }
};
var result = _collection.Aggregate()
.Group(group)
.ToListAsync().Result;
我从这个例子Mongodb aggregate not grouping documents by date找到了答案
var group = new BsonDocument { { "_id", new BsonDocument
{
{ "Month", new BsonDocument("$month", "$DateEntered") },
{ "Day", new BsonDocument("$dayOfMonth", "$DateEntered") },
{ "Year", new BsonDocument("$year", "$DateEntered") },
{ "Role", "$Role" },
}
}, { "Count", new BsonDocument("$sum", 1) } };