BsonDocument 基础数学(求和)MongoDB.Driver C#
BsonDocument basic math (sum) MongoDB.Driver C#
我有一组 BsonDocuments,如下所示:
{
"_id" : ObjectId("5699715218a323101c663b9a"),
"amount" : 24.32,
"color" : false
}
我想对这个特定集合中 "amount" 的所有值求和 (~1,000 BsonDocuments)。
这是我目前拥有的:
var group = new BsonDocument{
{ "$group", new BsonDocument
{
{ "_id", "$amount" },
}
}
};
var pipeline = new[] { group };
var result = collection.Aggregate(pipeline);`
声明变量 "result" 的最后一行给出了错误:"The type arguments for methods Aggregate cannot be inferred from the usage." 我真的觉得这个错误来自我如何设置 "group" BsonDocument 但实际上我不确定这是否是我应该前进的方向。感谢任何帮助。
感谢您的宝贵时间。
[编辑] Maximilianos Rios 的回答有效。此外,如果要计算处理的文档数,可以按以下方式完成:
var aggregation = collection.Aggregate<BsonDocument>()
.Group(new BsonDocument
{
{ "_id", BsonNull.Value
},
{
"total_amount", new BsonDocument
{
{
"$sum", "$amount"
}
}
},
{
"sum", new BsonDocument
{
{
"$sum", 1
}
}
}
});
var doc = aggregation.Single();
BsonDocument result = doc.AsBsonDocument;
var total = result["total_amount"].AsDouble;
var count = result["sum"].AsInt32;
首先,根据你的问题,你想做的是:
db.coll.aggregate([ { $group: { _id:null, total_amount:{"$sum":"$amount"}} } ])
引用你的话
I would like to sum all of the values for "amount" in this particular
collection
解决办法是像这样使用聚合框架的group方法:
var aggregation = collection.Aggregate<BsonDocument>()
.Group(new BsonDocument
{
{ "_id", BsonNull.Value
},
{
"total_amount", new BsonDocument
{
{
"$sum", "$amount"
}
}
}
});
var doc = aggregation.Single();
BsonDocument result = doc.AsBsonDocument;
var total = result["total_amount"].AsDouble;
请记住,您也可以异步获取值。
var doc = aggregation.SingleAsync();
我有一组 BsonDocuments,如下所示:
{
"_id" : ObjectId("5699715218a323101c663b9a"),
"amount" : 24.32,
"color" : false
}
我想对这个特定集合中 "amount" 的所有值求和 (~1,000 BsonDocuments)。
这是我目前拥有的:
var group = new BsonDocument{
{ "$group", new BsonDocument
{
{ "_id", "$amount" },
}
}
};
var pipeline = new[] { group };
var result = collection.Aggregate(pipeline);`
声明变量 "result" 的最后一行给出了错误:"The type arguments for methods Aggregate cannot be inferred from the usage." 我真的觉得这个错误来自我如何设置 "group" BsonDocument 但实际上我不确定这是否是我应该前进的方向。感谢任何帮助。
感谢您的宝贵时间。
[编辑] Maximilianos Rios 的回答有效。此外,如果要计算处理的文档数,可以按以下方式完成:
var aggregation = collection.Aggregate<BsonDocument>()
.Group(new BsonDocument
{
{ "_id", BsonNull.Value
},
{
"total_amount", new BsonDocument
{
{
"$sum", "$amount"
}
}
},
{
"sum", new BsonDocument
{
{
"$sum", 1
}
}
}
});
var doc = aggregation.Single();
BsonDocument result = doc.AsBsonDocument;
var total = result["total_amount"].AsDouble;
var count = result["sum"].AsInt32;
首先,根据你的问题,你想做的是:
db.coll.aggregate([ { $group: { _id:null, total_amount:{"$sum":"$amount"}} } ])
引用你的话
I would like to sum all of the values for "amount" in this particular collection
解决办法是像这样使用聚合框架的group方法:
var aggregation = collection.Aggregate<BsonDocument>()
.Group(new BsonDocument
{
{ "_id", BsonNull.Value
},
{
"total_amount", new BsonDocument
{
{
"$sum", "$amount"
}
}
}
});
var doc = aggregation.Single();
BsonDocument result = doc.AsBsonDocument;
var total = result["total_amount"].AsDouble;
请记住,您也可以异步获取值。
var doc = aggregation.SingleAsync();