diff --git a/lib/cursor.js b/lib/cursor.js index 0917563..673d7a4 100755 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -85,18 +85,16 @@ Cursor.prototype.project = function (candidates) { candidates.forEach(function (candidate) { var toPush; if (action === 1) { // pick-type projection - toPush = _.pick(candidate, keys); + toPush = { $set: {} }; + keys.forEach(function (k) { + toPush.$set[k] = model.getDotValue(candidate, k); + if (toPush.$set[k] === undefined) { delete toPush.$set[k]; } + }); + toPush = model.modify({}, toPush); } else { // omit-type projection - //console.log('--------------------------------------------'); - //console.log(candidate); toPush = { $unset: {} }; keys.forEach(function (k) { toPush.$unset[k] = true }); - - //console.log(toPush); - toPush = model.modify(candidate, toPush); - - //console.log(toPush); } if (keepId) { toPush._id = candidate._id; diff --git a/test/cursor.test.js b/test/cursor.test.js index aaa1bef..3bffa4a 100755 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -834,6 +834,21 @@ describe('Cursor', function () { }); }); + it("Projections on embedded documents - pick type", function (done) { + var cursor = new Cursor(d, {}); + cursor.sort({ age: 1 }); // For easier finding + cursor.projection({ name: 1, 'toys.ballon': 1, _id: 0 }); + cursor.exec(function (err, docs) { + assert.deepEqual(docs[0], { name: 'Jo', toys: { ballon: 'much' } }); + assert.deepEqual(docs[1], { name: 'LM' }); + assert.deepEqual(docs[2], { name: 'Grafitti' }); + assert.deepEqual(docs[3], { name: 'Louis', toys: { ballon: 'yeah' } }); + assert.deepEqual(docs[4], {}); + + done(); + }); + }); + }); // ==== End of 'Projections' ==== });