Skip to content

Commit

Permalink
fix(query): pass projection fields to cursor
Browse files Browse the repository at this point in the history
Fix #4342
  • Loading branch information
Corei13 committed Jul 29, 2016
1 parent 9aa4257 commit 88ec4af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/query.js
Expand Up @@ -2873,6 +2873,7 @@ Query.prototype.stream = util.deprecate(Query.prototype.stream, 'Mongoose: ' +
Query.prototype.cursor = function cursor(opts) {
this._applyPaths();
this._fields = this._castFields(this._fields);
this.setOptions({ fields: this._fieldsForExec() });
if (opts) {
this.setOptions(opts);
}
Expand Down
29 changes: 29 additions & 0 deletions test/query.cursor.test.js
Expand Up @@ -72,6 +72,35 @@ describe('QueryCursor', function() {
});
});

it('with projection', function(done) {
var personSchema = new Schema({
name: String,
born: String
});
var Person = db.model('Person4342', personSchema);
var people = [
{ name: 'Axl Rose', born: 'William Bruce Rose' },
{ name: 'Slash', born: 'Saul Hudson' }
];
Person.create(people, function(error) {
assert.ifError(error);
var cursor = Person.find({}, { _id: 0, name: 1 }).sort({ name: 1 }).cursor();
cursor.next(function(error, doc) {
assert.ifError(error);
assert.equal(doc._id, undefined);
assert.equal(doc.name, 'Axl Rose');
assert.equal(doc.born, undefined);
cursor.next(function(error, doc) {
assert.ifError(error);
assert.equal(doc._id, undefined);
assert.equal(doc.name, 'Slash');
assert.equal(doc.born, undefined);
done();
});
});
});
});

it('with populate', function(done) {
var bandSchema = new Schema({
name: String,
Expand Down

0 comments on commit 88ec4af

Please sign in to comment.