使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk

编程入门 行业动态 更新时间:2024-10-26 17:29:01
本文介绍了使用 MongoDB C# 驱动程序在聚合框架中使用 allowDisk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想允许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

本文发布于:2023-11-22 09:39:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:驱动程序   框架   MongoDB   allowDisk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!