Skip to content

Commit

Permalink
Merge pull request #863 from aheckmann/862-2.x
Browse files Browse the repository at this point in the history
[ready] fixed; incorrect reported num of affected docs in update ops (2.x)
  • Loading branch information
rauchg committed Apr 25, 2012
2 parents 2d41f2b + 9f8464f commit 0dc5333
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ Query.prototype.update = function update (doc, callback) {
model.collection.update(castQuery, castDoc, options, tick(callback));
} else {
process.nextTick(function () {
callback(null);
callback(null, 0);
});
}

Expand All @@ -1254,7 +1254,6 @@ Query.prototype._castUpdate = function _castUpdate (obj) {

while (i--) {
var op = ops[i];
hasKeys = true;
if ('$' !== op[0]) {
// fix up $set sugar
if (!ret.$set) {
Expand Down Expand Up @@ -1283,7 +1282,7 @@ Query.prototype._castUpdate = function _castUpdate (obj) {
op = ops[i];
val = ret[op];
if ('Object' === val.constructor.name) {
this._walkUpdatePath(val, op);
hasKeys |= this._walkUpdatePath(val, op);
} else {
var msg = 'Invalid atomic update value for ' + op + '. '
+ 'Expected an object, received ' + typeof val;
Expand All @@ -1301,6 +1300,7 @@ Query.prototype._castUpdate = function _castUpdate (obj) {
* @param {Object} obj - part of a query
* @param {String} op - the atomic operator ($pull, $set, etc)
* @param {String} pref - path prefix (internal only)
* @return {Bool} true if this path has keys to update
* @private
*/

Expand All @@ -1309,6 +1309,7 @@ Query.prototype._walkUpdatePath = function _walkUpdatePath (obj, op, pref) {
, prefix = pref ? pref + '.' : ''
, keys = Object.keys(obj)
, i = keys.length
, hasKeys = false
, schema
, key
, val
Expand All @@ -1327,6 +1328,7 @@ Query.prototype._walkUpdatePath = function _walkUpdatePath (obj, op, pref) {
// path is not in our strict schema. do not include
delete obj[key];
} else {
hasKeys = true;
if ('$each' in val) {
obj[key] = {
$each: this._castUpdateVal(schema, val.$each, op)
Expand All @@ -1336,7 +1338,7 @@ Query.prototype._walkUpdatePath = function _walkUpdatePath (obj, op, pref) {
}
}
} else {
this._walkUpdatePath(val, op, prefix + key);
hasKeys |= this._walkUpdatePath(val, op, prefix + key);
}
} else {
schema = '$each' === key
Expand All @@ -1350,10 +1352,12 @@ Query.prototype._walkUpdatePath = function _walkUpdatePath (obj, op, pref) {
if (skip) {
delete obj[key];
} else {
hasKeys = true;
obj[key] = this._castUpdateVal(schema, val, op, key);
}
}
}
return hasKeys;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/model.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ module.exports = {
db.close();

var doc = S.find()._castUpdate({ ignore: true });
Object.keys(doc.$set).length.should.equal(0);
should.eql(false, doc)
var doc = S.find()._castUpdate({ $unset: {x: 1}});
Object.keys(doc.$unset).length.should.equal(1);
},
Expand All @@ -499,7 +499,7 @@ module.exports = {

S.update({ _id: s._id }, { ignore: true }, function (err, affected) {
should.strictEqual(null, err);
affected.should.equal(1);
affected.should.equal(0);

S.findById(s._id, function (err, doc) {
db.close();
Expand Down

0 comments on commit 0dc5333

Please sign in to comment.