Updating more than one MongoDB document in NodeJS doesn't seem to work

后端 未结 1 1837
死守一世寂寞
死守一世寂寞 2021-01-24 08:40

So I know this question has been asked many times but I\'ve looked it up for over 30 minutes and I honestly can\'t find what I\'m doing wrong.

I want to update my mongo

相关标签:
1条回答
  • 2021-01-24 09:06

    As noted in the docs for update, if you don't want to provide a callback, you need to call exec on the returned Query to execute it:

    To update documents without waiting for a response from MongoDB, do not pass a callback, then call exec on the returned Query

    So either chain an exec call on your update or provide a callback:

    function updateUsers(){
        UserModel.update({}, {$set: {userName: 'JOHN CENA'}}, {multi:true}).exec();
    }
    

    OR

    function updateUsers(){
        UserModel.update({}, {$set: {userName: 'JOHN CENA'}}, {multi:true},
            function(err, numAffected) {...});
    }
    
    0 讨论(0)
提交回复
热议问题