【发布时间】:2016-01-16 18:23:57
【问题描述】:
我有一个 BsonDocuments 集合,每个 BsonDocuments 都有一个名为“engagement”的字段(它是一个数字)和一个名为“color”的字段。我想要 "color" = "blue" 的所有 BsonDocuments 的总成本。
到目前为止,我发现我需要做这样的事情:
var collection = db.GetCollection<BsonDocument>("collectionName");
var match = new BsonDocument
{
{
"$match",
new BsonDocument
{
{"sentiment", "positive"}
}
}
};
var group = new BsonDocument
{
{ "$group",
new BsonDocument
{
{
"Sum", new BsonDocument
{
{
"$sum", "$engagement"
}
}
}
}
}
};
var pipeline = new[] { match, group };
var result = collection.Aggregate(pipeline);
我收到关于无法从使用中推断聚合方法的类型方法参数的错误。
简单地说,我想知道如何使用聚合框架执行简单的数学运算。在此示例中,我尝试将我的集合中所有 BsonDocuments 中的参与字段的所有值相加。如果可能的话,Builder 函数的使用会很好,但是手动创建的 BsonDocument 也可以使用。
感谢您的宝贵时间
【问题讨论】: