MongoDB: update every document on one field

后端 未结 4 1420
温柔的废话
温柔的废话 2020-11-27 09:49

I have a collected named foo hypothetically.

Each instance of foo has a field called lastLookedAt which is a UNIX timestamp since epoch. I\

4条回答
  •  -上瘾入骨i
    2020-11-27 10:23

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

    Simple query is like this

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

    For more doc of uppdateMany read here

    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

提交回复
热议问题