问题描述
我想允许DiskUse:true.但是我找不到任何示例来解释为 MongoDB C# 驱动程序启用 allowDiskUse.
I would like to allowDiskUse:true. However I could not found any example which explain allowDiskUse enabling for MongoDB C# driver.
如何在 MongoDB C# 驱动程序中启用 allowDiskUse?
How can I enable allowDiskUse in MongoDB C# driver?
我的示例代码就是这样
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();
推荐答案
使用 Aggregate 的另一个重载,该重载采用 AggregateArgs 参数并为您提供对操作的更多控制,包括设置 AllowDiskUse:
Use the other overload of Aggregate that takes an AggregateArgs parameter and gives you more control over the operation, including setting 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<BsonDocument>因此您不再需要使用 ResultDocuments 属性.
Note that the return type of this overload of Aggregate is IEnumerable<BsonDocument> so you no longer have to use the ResultDocuments property.
为了清楚起见,Select 正在客户端执行.您也许可以安排它,以便可以将来自聚合管道的文档直接反序列化为您的某个类的实例.
Just to be clear, the Select is being executed client side. You might be able to arrange it so that the documents coming out of your aggregation pipeline can be directly deserialized into instances of one of your classes.
这篇关于使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!