Dre*_*her 5 c# mongodb aggregation-framework mongodb-.net-driver
我想允许DiskUse:true。但是,我找不到任何解释为 MongoDB C# 驱动程序启用 allowDiskUse 的示例。
如何在 MongoDB C# 驱动程序中启用 allowDiskUse?
我的示例代码是这样的
var pipeline = new[] { match, project, group, limit, sort, allow };
List<SMBMostInfluentialUser> result = db
.GetCollection<SMBTwitterStatus>("TwitterStatus")
.Aggregate(pipeline).ResultDocuments.Select(x =>
new User
{
Influence = Convert.ToDouble(x["Influence"]),
User = new SMBUser((BsonDocument)x["User"])
}).ToList();
Run Code Online (Sandbox Code Playgroud)
对于更新版本的 MongoDB C# 驱动程序(不确定从哪个版本开始),语法为:
var aggregateOptions = new AggregateOptions{ AllowDiskUse = true};
var aggregateResult = collection.Aggregate(aggregateOptions);
Run Code Online (Sandbox Code Playgroud)
使用 Aggregate 的另一个重载,它采用 AggregateArgs 参数并让您更好地控制操作,包括设置 AllowDiskUse:
var pipeline = new BsonDocument[0]; // replace with a real pipeline
var aggregateArgs = new AggregateArgs { AllowDiskUse = true, Pipeline = pipeline };
var aggregateResult = collection.Aggregate(aggregateArgs);
var users = aggregateResult.Select(x =>
new User
{
Influence = x["Influence"].ToDouble(),
User = new SMBUser(x["user"].AsBsonDocument)
}).ToList();
Run Code Online (Sandbox Code Playgroud)
请注意,此 Aggregate 重载的返回类型是 IEnumerable<BsonDocument>,因此您不再需要使用 ResultDocuments 属性。
需要明确的是,Select 正在客户端执行。您也许可以对其进行安排,以便可以将来自聚合管道的文档直接反序列化为您的类之一的实例。