Can we always use allowDiskUse:true in MongoDB aggregation query

Viewed 2144

Aggregation pipeline stages have a limit of 100 MB. To handle large datasets, allowDiskUse enables writing to temporary files. But have this optional any defect? If haven't, why the default is false?

1 Answers

Unless you get an out-of-memory error during aggregation stages like sort, you don't need to use this option.

Note that working with data in memory is very much faster than working with data on hard drive. With hard drive data transfer the aggregation operation has to keep transferring data between the operation, memory and the hard disk.

So, by default allowDiskUse option is disabled (or set tofalse). You need to use this option only when needed - e.g., your sort operation needs more than 100 MB RAM and there is an error. Then you will have to use allowDiskUse:true to do your aggregation without errors. In such a case, note that when this option is used (set to true) the aggreagation operation will be slower.

If your aggregation is working fine and using this option allowDiskUse:true has no effect as the operation is able use the memory effectively (and the aggregation ignores this option).

Related