Can pick type project on embedded documents

pull/2/head
Louis Chatriot 9 years ago
parent fae4504793
commit 0eeca73bb0
  1. 14
      lib/cursor.js
  2. 15
      test/cursor.test.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;

@ -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' ====
});

Loading…
Cancel
Save