【发布时间】:2014-11-21 23:32:33
【问题描述】:
我开始使用 Mongo 客户端做一些漂亮的查询和聚合.. 但现在我想在 .NET/C# 中使用它,我发现我不能简单地将查询作为文本字段运行..
此外,在求助于构建聚合管道并运行 collection.Aggregate() 函数后,我得到了一个结果集,但我不知道如何遍历它..
任何人都可以在这里帮助指导我吗?
这是我的代码:
var coll = db.GetCollection("animals");
var match = new BsonDocument {
{ "$match", new BsonDocument {{"category","cats"}} }
};
var group = new BsonDocument{
{
"$group", new BsonDocument{
{"_id", "$species"},
{"AvgWeight", new BsonDocument{{"$avg", "$weight"}}} }
}
};
var sort = new BsonDocument{{"$sort", new BsonDocument{{"AvgWeight", -1}}}};
var pipeline = new[] { match, group, sort };
var args = new AggregateArgs { Pipeline = pipeline };
var res = coll.Aggregate(args);
foreach (var obj in res)
{
// WHAT TO DO HERE??
}
另外,我应该说我对 C# / ASP.NET / MVC 有点生疏,因此非常感谢任何简化的空间。
【问题讨论】:
-
如果可能的话,输入你的输出结果
标签: c# asp.net-mvc mongodb