MongoDb 聚合后计数 C# 2.0 驱动程序
MongoDb Count after aggregation C# 2.0 driver
我有以下聚合管道
var count = dbCollection.
Aggregate(new AggregateOptions { AllowDiskUse = true }).Match(query).
Group(groupby).
ToListAsync().Result.Count();
这会得到以下结果:
{
"result" : [
{
"_id" : {
"ProfileId" : ObjectId("55f6c727965bb016c81971ba")
}
},
{
"_id" : {
"ProfileId" : ObjectId("55f6c727965bb016c81971bb")
}
}
],
"ok" : 1
}
但是好像会在client上进行count操作,但是在MongoDb
中如何进行呢?
我有 MongoDb 2.0 C# driver
& MongoDb v. 3.0.2
将常量字段添加到您的分组函数,然后在常量字段上再次分组(以便所有结果分组到一个组中),总和为 1。第一个(也是唯一一个)结果将具有总和。
例如。
var count = dbCollection.
Aggregate(new AggregateOptions { AllowDiskUse = true }).Match(query).
Group(groupby).Group(<id>:{ConstantField},Total:{$sum:1})
ToListAsync().Result.First().GetValue("Total").
我有以下聚合管道
var count = dbCollection.
Aggregate(new AggregateOptions { AllowDiskUse = true }).Match(query).
Group(groupby).
ToListAsync().Result.Count();
这会得到以下结果:
{
"result" : [
{
"_id" : {
"ProfileId" : ObjectId("55f6c727965bb016c81971ba")
}
},
{
"_id" : {
"ProfileId" : ObjectId("55f6c727965bb016c81971bb")
}
}
],
"ok" : 1
}
但是好像会在client上进行count操作,但是在MongoDb
中如何进行呢?
我有 MongoDb 2.0 C# driver
& MongoDb v. 3.0.2
将常量字段添加到您的分组函数,然后在常量字段上再次分组(以便所有结果分组到一个组中),总和为 1。第一个(也是唯一一个)结果将具有总和。
例如。
var count = dbCollection.
Aggregate(new AggregateOptions { AllowDiskUse = true }).Match(query).
Group(groupby).Group(<id>:{ConstantField},Total:{$sum:1})
ToListAsync().Result.First().GetValue("Total").