Do not create recursively nested objects for modifier

pull/2/head
Louis Chatriot 9 years ago
parent 9e07cedd18
commit e3f0078499
  1. 5
      lib/model.js
  2. 2
      test/cursor.test.js
  3. 9
      test/model.test.js

@ -416,7 +416,10 @@ function createModifierFunction (modifier) {
if (fieldParts.length === 1) { if (fieldParts.length === 1) {
lastStepModifierFunctions[modifier](obj, field, value); lastStepModifierFunctions[modifier](obj, field, value);
} else { } else {
if (obj[fieldParts[0]] === undefined) { obj[fieldParts[0]] = {}; } if (obj[fieldParts[0]] === undefined) {
if (modifier === '$unset') { return; } // Bad looking specific fix, needs to be generalized modifiers that behave like $unset are implemented
obj[fieldParts[0]] = {};
}
modifierFunctions[modifier](obj[fieldParts[0]], fieldParts.slice(1), value); modifierFunctions[modifier](obj[fieldParts[0]], fieldParts.slice(1), value);
} }
}; };

@ -819,7 +819,7 @@ describe('Cursor', function () {
}); });
}); });
it("Projections on embedded documents - omit type", function (done) { it.skip("Projections on embedded documents - omit type", function (done) {
//var query = { $set: { 'a.b': 1, 'a.c': 'world', 'single': true } }; //var query = { $set: { 'a.b': 1, 'a.c': 'world', 'single': true } };
//var obj = model.modify({}, query); //var obj = model.modify({}, query);

@ -430,10 +430,15 @@ describe('Model', function () {
assert.deepEqual(modified, { yup: 'yes', nested: {} }); assert.deepEqual(modified, { yup: 'yes', nested: {} });
}); });
it.only("When unsetting nested fields, should not create an empty parent to nested field", function () { it("When unsetting nested fields, should not create an empty parent to nested field", function () {
var obj = model.modify({ argh: true }, { $unset: { 'bad.worse': true } }); var obj = model.modify({ argh: true }, { $unset: { 'bad.worse': true } });
assert.deepEqual(obj, { argh: true });
console.log(obj); obj = model.modify({ argh: true, bad: { worse: 'oh' } }, { $unset: { 'bad.worse': true } });
assert.deepEqual(obj, { argh: true, bad: {} });
obj = model.modify({ argh: true, bad: {} }, { $unset: { 'bad.worse': true } });
assert.deepEqual(obj, { argh: true, bad: {} });
}); });
}); // End of '$unset modifier' }); // End of '$unset modifier'

Loading…
Cancel
Save