Permalink
Browse files

Can pick type project on embedded documents

  • Loading branch information...
1 parent fae4504 commit 0eeca73bb0e313e0d80dafa510a25e6655b0041c @louischatriot committed Feb 14, 2016
Showing with 21 additions and 8 deletions.
  1. +6 −8 lib/cursor.js
  2. +15 −0 test/cursor.test.js
View
@@ -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;
View
@@ -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' ====
});

0 comments on commit 0eeca73

Please sign in to comment.