使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk
Posted
技术标签:
【中文标题】使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk【英文标题】:allowDiskUse in Aggregation Framework with MongoDB C# Driver 【发布时间】:2022-01-20 00:09:23 【问题描述】:我想允许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();
【问题讨论】:
在此api.mongodb.org/csharp/current/html/… 页面的“属性”部分中,您需要将参数设置为 true 以允许使用磁盘。在执行 db.GetCollection... 查询之前将其设置为 true。 【参考方案1】:使用 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();
请注意,此 Aggregate 重载的返回类型是 IEnumerable
为了清楚起见,Select 正在客户端执行。您也许可以对其进行安排,以便可以将来自聚合管道的文档直接反序列化为您的某个类的实例。
【讨论】:
【参考方案2】:对于 MongoDB C# 驱动程序的更新版本(不确定从哪个版本开始),语法为:
var aggregateOptions = new AggregateOptions AllowDiskUse = true;
var aggregateResult = collection.Aggregate(aggregateOptions);
【讨论】:
以上是关于使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk的主要内容,如果未能解决你的问题,请参考以下文章