Is there any way to supply the allowDiskUse option to an mongoose.js aggregation?
Asked Answered
A

4

20

I'm running an aggregation on a collection of around 300k+ records which requires several unwinds and regroups. I'm hitting the following error;

'exception: Exceeded memory limit for $group, but didn\'t allow external sort. Pass allowDiskUse:true to opt in.'

I can't seem to work out how to pass this option through using the mongoose.js API?

Affiance answered 27/5, 2014 at 16:5 Comment(0)
W
23

We don't have a helper for this right now, but an allowDiskUse() helper function will be included in Mongoose 3.8.12, which I'll ship today: https://github.com/LearnBoost/mongoose/issues/2114

If you want an immediate solution or don't want to upgrade to 3.8.12 (although upgrading is recommended), you can do something like:

var aggregation = MyModel.aggregate(...); 
aggregation.options = { allowDiskUse: true }; 
aggregation.exec(function() {});
Wilding answered 30/5, 2014 at 19:0 Comment(1)
how to use with promises?Horsy
G
24
Model.aggregate(..).allowDiskUse(true).exec(callback)

mongoose api

Gavrila answered 9/10, 2016 at 7:52 Comment(0)
W
23

We don't have a helper for this right now, but an allowDiskUse() helper function will be included in Mongoose 3.8.12, which I'll ship today: https://github.com/LearnBoost/mongoose/issues/2114

If you want an immediate solution or don't want to upgrade to 3.8.12 (although upgrading is recommended), you can do something like:

var aggregation = MyModel.aggregate(...); 
aggregation.options = { allowDiskUse: true }; 
aggregation.exec(function() {});
Wilding answered 30/5, 2014 at 19:0 Comment(1)
how to use with promises?Horsy
P
3
const agg = Model.aggregate(..).option({ allowDiskUse: true });

worked for me as per the Mongoose V6.0.4 documentation here

Pleinair answered 6/9, 2021 at 17:33 Comment(0)
R
0

const data = await Model.aggregate([..]).option({ allowDiskUse: true }).explain();

Source - Mongoose explain API

Ribonuclease answered 2/2 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.