Skip to content

Commit

Permalink
Can pick type project on embedded documents
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Feb 14, 2016
1 parent fae4504 commit 0eeca73
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ====

});
Expand Down

0 comments on commit 0eeca73

Please sign in to comment.