0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so it actually should work. What confuses me is that the docs define the update method as: Query#update([criteria], [doc], [options], [callback]) but then sometimes use it without the criteria and directly pass the doc: Model.where({ _id: id }).update({ title: 'words' })
@velop The update will use the query criteria already added to the Query (e.g. from the where call in your example) if you don't provide it in the update call itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.