Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

allowDiskUse in Aggregation Framework with MongoDB C# Driver

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();
like image 678
Dreamcatcher Avatar asked Jul 20 '14 22:07

Dreamcatcher


People also ask

How do I use allowDiskUse in MongoDB?

Use allowDiskUse() to either allow or prohibit writing temporary files on disk when a pipeline stage exceeds the 100 megabyte limit. Starting in MongoDB 6.0, operations that require greater than 100 megabytes of memory automatically write data to temporary files by default.

Is MongoDB good at aggregation?

As with many other database systems, MongoDB allows you to perform a variety of aggregation operations. These allow you to process data records in a variety of ways, such as grouping data, sorting data into a specific order, or restructuring returned documents, as well as filtering data as one might with a query.

Is aggregation a framework in MongoDB?

The MongoDB aggregation framework is an extremely powerful set of tools. The processing is done on the server itself which results in less data being sent over the network.

Which aggregation method is preferred for use by MongoDB?

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection.


2 Answers

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.

like image 172
Robert Stam Avatar answered Sep 27 '22 21:09

Robert Stam


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);
like image 42
July.Tech Avatar answered Sep 27 '22 21:09

July.Tech