velop
velop

Reputation: 3224

Mongodb query.update method does not update array

Previously I had tried something like (with mongoose and promises):

var cursor = User.find({email: from.address, token: tokenMatches[1]}); and then

return cursor.update(
    {'votes.title': b},
    {'$set': { 'votes.$.category': a }}
).then(function (result) {
    if(result.nModified == 0) {
        return cursor.update({}, {'$push': { votes: { category: a, title: b }}}).then(function (res) {
            ServerLog('updatePush', res);
            return res;
        });
    }
});

But it always returned nModified = 0 for the first and second call. Until I found out that the cursor object actually has no update function. So why is it so? And why did it not throw an exception?

Upvotes: 0

Views: 180

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312035

Model.find returns a Query object, not a cursor. Query does have an update method that lets you execute the query as an update operation.

Upvotes: 2

Related Questions