Reputation: 1106
I'm trying to maintain an instantaneous statistics block, e.g.;
model = {
id: 123,
stats: {
fooCount: 117,
barCount: 175,
bazCount: 654
}
}
...and be able to update an existing record, but only specify the new values, like this:
model.update({
'_id': 123,
stats: {
fooCount: 118
}
});
...without blowing away the old values in stats
. Is this possible without writing too much more code around my updates?
Upvotes: 4
Views: 5024
Reputation: 1549
You should be using a $set operator in your update like so:
var conditions = { _id: 123 };
var update = { $set: { fooCount: 118 }};
var options = { upsert: true };
model.update(conditions, update, options, callback);
There are many more operators that will be of interest on that MongoDB page as well.
Upvotes: 6