allowDiskUse in Aggregation Framework with MongoDB C# Driver

浪尽此生 提交于 2019-12-10 17:33:12

问题


I would like to allowDiskUse:true. However I could not found any example which explain allowDiskUse enabling for MongoDB C# driver.

How can I enable allowDiskUse in MongoDB C# driver?

My sample code like that

    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();

回答1:


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();

Note that the return type of this overload of Aggregate is IEnumerable<BsonDocument> so you no longer have to use the ResultDocuments property.

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.




回答2:


For more recent versions of MongoDB C# driver (not sure starting with what version), the syntax is:

var aggregateOptions = new AggregateOptions{ AllowDiskUse = true};
var aggregateResult = collection.Aggregate(aggregateOptions);


来源:https://stackoverflow.com/questions/24855696/allowdiskuse-in-aggregation-framework-with-mongodb-c-sharp-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!