yandex
loader

please wait

  • Yasmin Mirza Mar-11-2019 12:49:06 PM ( 2 months ago )

    I have a very simple case. I want to update my collection every midnight. Im using node-schedule:

     

    schedule.scheduleJob('0 0 * * *', () => {
       Users.updateMany();
    });

     

    All I want to do, is to loop over every document in my collection (Users) and then if User.created is false, I want to turn it into true.

     

    In javascript it would be:

     

    for (let user in Users) {
       if (user.created === false) {
          user.created = true;
       }
    } 

    How to do it in mongoose?

  • Vikrant Srivastava Mar-11-2019 12:50:38 PM ( 2 months ago )

    You can use updateMany() methods of mongodb to update multiple document

    Simple query is like this

    db.collection.updateMany(filter, update, options)

    As per your requirement the update code will be like this:

    User.updateMany({"created": false}, {"$set":{"created": true}});

    here you need to use $set because you just want to change created from true to false. For ref. If you want to change entire doc then you don't need to use $set

     

Please login

Similar Discussion

Recommended For You