$setOnInsert in minimongo

This commit is contained in:
David Greenspan
2013-09-27 13:53:09 -07:00
parent e1aa2450e9
commit d8d9206f4b
2 changed files with 5 additions and 17 deletions

View File

@@ -549,19 +549,12 @@ LocalCollection.prototype.remove = function (selector, callback) {
// we rollback the whole operation, or what?
LocalCollection.prototype.update = function (selector, mod, options, callback) {
var self = this;
var newMod = _.clone(mod);
if (! callback && options instanceof Function) {
callback = options;
options = null;
}
if (!options) options = {};
var setOnInsert;
if (newMod.$setOnInsert) {
setOnInsert = _.clone(newMod.$setOnInsert);
delete newMod.$setOnInsert;
}
var selector_f = LocalCollection._compileSelector(selector);
// Save the original results of any query that we might need to
@@ -583,7 +576,7 @@ LocalCollection.prototype.update = function (selector, mod, options, callback) {
if (selector_f(doc)) {
// XXX Should we save the original even if mod ends up being a no-op?
self._saveOriginal(id, doc);
self._modifyAndNotify(doc, newMod, recomputeQids);
self._modifyAndNotify(doc, mod, recomputeQids);
++updateCount;
if (!options.multi)
break;
@@ -598,18 +591,10 @@ LocalCollection.prototype.update = function (selector, mod, options, callback) {
});
self._observeQueue.drain();
if (setOnInsert)
newMod.$setOnInsert = setOnInsert;
var insertedId;
if (updateCount === 0 && options.upsert) {
var newDoc = _.clone(selector);
if (newMod.$setOnInsert) {
newMod.$set = _.extend(newMod.$set || {},
_.clone(newMod.$setOnInsert));
delete newMod.$setOnInsert;
}
LocalCollection._modify(newDoc, newMod);
LocalCollection._modify(newDoc, mod, true);
insertedId = self.insert(newDoc);
updateCount = 1;
}

View File

@@ -147,6 +147,9 @@ LocalCollection._modifiers = {
target[field] = EJSON.clone(arg);
},
$setOnInsert: function (target, field, arg) {
// converted to `$set` in `_modify`
},
$unset: function (target, field, arg) {
if (target !== undefined) {
if (target instanceof Array) {