Reputation: 2664
From mongodb cursor limit's docs
A limit() value of 0 (i.e. .limit(0)) is equivalent to setting no limit.
but I cant find way to reset query limit in mongodb aggregation limit docs.
const listQuery = query.limit(20).exec(); // I need limit here
query.limit(0); // error here
const totalQuery = query.group(....).exec(); // No limit must be specified
I am getting error
MongoError: the limit must be positive
Thanks for replies!
Upvotes: 1
Views: 3517
Reputation: 2664
I wrote function which resets any pipeline command from aggregation
Aggregate.prototype.reset = function (command, onlyLatest) {
command = command.replace(/^/, '$');
let i = this._pipeline.length;
while (i--) {
if (command === Object.keys(this._pipeline[i])[0]) {
this._pipeline.splice(i, 1);
if (onlyLatest) {
break;
}
}
}
return this;
};
Example, useful when you have pagination
var query = this.aggregate();
query.match({ ... });
query.lookup({ ... });
query.unwind({ ... });
query.project({ ... });
query.sort(...);
query.skip();
query.limit(10);
const listQuery = query.exec();
const totalQuery = query.reset('limit').reset('skip').exec();
return Promise.all([listQuery, totalQuery]);
Upvotes: 1