diff --git a/lib/cursor.js b/lib/cursor.js index 799de30..20c6bb1 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -84,7 +84,11 @@ Cursor.prototype.project = function (candidates) { // Do the actual projection candidates.forEach(function (candidate) { var toPush = action === 1 ? _.pick(candidate, keys) : _.omit(candidate, keys); - if (keepId) { toPush._id = candidate._id; } + if (keepId) { + toPush._id = candidate._id; + } else { + delete toPush._id; + } res.push(toPush); }); @@ -161,7 +165,7 @@ Cursor.prototype._exec = function(callback) { res = this.project(res); } catch (e) { error = e; - res = null; + res = undefined; } if (this.execFn) { diff --git a/test/cursor.test.js b/test/cursor.test.js index fa1242b..c632b45 100644 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -722,18 +722,57 @@ describe('Cursor', function () { cursor.exec(function (err, docs) { assert.isNull(err); docs.length.should.equal(5); - // Takes the _id by default - assert.deepEqual(docs[0], { age: 5, name: 'Jo', _id: doc0._id }); - assert.deepEqual(docs[1], { age: 23, name: 'LM', _id: doc3._id }); - assert.deepEqual(docs[2], { age: 52, name: 'Grafitti', _id: doc2._id }); - assert.deepEqual(docs[3], { age: 57, name: 'Louis', _id: doc1._id }); - assert.deepEqual(docs[4], { age: 89, _id: doc4._id }); // No problems if one field to take doesn't exist + assert.deepEqual(docs[0], { age: 5, name: 'Jo' }); + assert.deepEqual(docs[1], { age: 23, name: 'LM' }); + assert.deepEqual(docs[2], { age: 52, name: 'Grafitti' }); + assert.deepEqual(docs[3], { age: 57, name: 'Louis' }); + assert.deepEqual(docs[4], { age: 89 }); // No problems if one field to take doesn't exist done(); }); }); }); + it('Can omit only the expected fields', function (done) { + var cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); // For easier finding + cursor.projection({ age: 0, name: 0 }); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(5); + // Takes the _id by default + assert.deepEqual(docs[0], { planet: 'B', _id: doc0._id }); + assert.deepEqual(docs[1], { planet: 'S', _id: doc3._id }); + assert.deepEqual(docs[2], { planet: 'C', _id: doc2._id }); + assert.deepEqual(docs[3], { planet: 'R', _id: doc1._id }); + assert.deepEqual(docs[4], { planet: 'Earth', _id: doc4._id }); + + cursor.projection({ age: 0, name: 0, _id: 0 }); + cursor.exec(function (err, docs) { + assert.isNull(err); + docs.length.should.equal(5); + assert.deepEqual(docs[0], { planet: 'B' }); + assert.deepEqual(docs[1], { planet: 'S' }); + assert.deepEqual(docs[2], { planet: 'C' }); + assert.deepEqual(docs[3], { planet: 'R' }); + assert.deepEqual(docs[4], { planet: 'Earth' }); + + done(); + }); + }); + }); + + it('Cannot use both modes except for _id', function (done) { + var cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); // For easier finding + cursor.projection({ age: 1, name: 0 }); + cursor.exec(function (err, docs) { + assert.isNotNull(err); + assert.isUndefined(docs); + done(); + }); + }); + }); // ==== End of 'Projections' ==== });