Actually implement minimongo upsert.

Got a little over-confident there...
This commit is contained in:
Emily Stark
2013-09-26 14:22:23 -07:00
parent 86565853b1
commit 0286395700

View File

@@ -473,6 +473,7 @@ LocalCollection.prototype.insert = function (doc, callback) {
LocalCollection._recomputeResults(self.queries[qid]);
});
self._observeQueue.drain();
// Defer because the caller likely doesn't expect the callback to be run
// immediately.
if (callback)
@@ -589,7 +590,20 @@ LocalCollection.prototype.update = function (selector, mod, options, callback) {
qidToOriginalResults[qid]);
});
self._observeQueue.drain();
var result = { numberAffected: updateCount };
var insertedId;
if (updateCount === 0 && options.upsert) {
var newDoc = _.clone(selector);
LocalCollection._modify(newDoc, mod);
insertedId = self.insert(newDoc);
updateCount = 1;
}
var result = {
numberAffected: updateCount
};
if (insertedId !== undefined)
result.insertedId = insertedId;
if (callback)
Meteor.defer(function () {
callback(null, result);