Skip to content

Commit

Permalink
Simplify adding pipeline stages
Browse files Browse the repository at this point in the history
  • Loading branch information
erikolson186 committed Feb 2, 2017
1 parent 0f7c971 commit da8d0b9
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions src/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Cursor extends EventEmitter {

fn(doc);

this.forEach(fn, cb);
this._forEach(fn, cb);
});
}

Expand Down Expand Up @@ -325,6 +325,13 @@ class Cursor extends EventEmitter {
return this;
}

_addStage(fn, arg) {
this._assertUnopened();
this._pipeline.push([fn, arg]);

return this;
}

/**
* Limit the number of documents that can be iterated.
* @param {number} num The limit.
Expand All @@ -333,12 +340,7 @@ class Cursor extends EventEmitter {
* @example
* col.find().limit(10);
*/
limit(num) {
this._assertUnopened();
this._pipeline.push([limit, num]);

return this;
}
limit(num) { return this._addStage(limit, num); }

/**
* Skip over a specified number of documents.
Expand All @@ -348,12 +350,7 @@ class Cursor extends EventEmitter {
* @example
* col.find().skip(4);
*/
skip(num) {
this._assertUnopened();
this._pipeline.push([skip, num]);

return this;
}
skip(num) { return this._addStage(skip, num); }

/**
* Add new fields, and include or exclude pre-existing fields.
Expand All @@ -363,12 +360,7 @@ class Cursor extends EventEmitter {
* @example
* col.find().project({ _id: 0, x: 1, n: { $add: ['$k', 4] } });
*/
project(spec) {
this._assertUnopened();
this._pipeline.push([project, spec]);

return this;
}
project(spec) { return this._addStage(project, spec); }

/**
* Group documents by an _id and optionally add computed fields.
Expand All @@ -382,12 +374,7 @@ class Cursor extends EventEmitter {
* count: { $sum: 1 }
* });
*/
group(spec) {
this._assertUnopened();
this._pipeline.push([group, spec]);

return this;
}
group(spec) { return this._addStage(group, spec); }

/**
* Deconstruct an iterable and output a document for each element.
Expand All @@ -397,12 +384,7 @@ class Cursor extends EventEmitter {
* @example
* col.find().unwind('$elements');
*/
unwind(path) {
this._assertUnopened();
this._pipeline.push([unwind, path]);

return this;
}
unwind(path) { return this._addStage(unwind, path); }

/**
* Sort documents.
Expand All @@ -426,12 +408,7 @@ class Cursor extends EventEmitter {
* // If x is indexed, it will be used for sorting.
* col.find().sort({ x: 1 }).hint('x');
*/
sort(spec) {
this._assertUnopened();
this._pipeline.push([sort, spec]);

return this;
}
sort(spec) { return this._addStage(sort, spec); }

_initSort(spec, clauses) {
const new_clauses = [];
Expand Down

0 comments on commit da8d0b9

Please sign in to comment.