MongoDB: update every document on one field

后端 未结 4 1422
温柔的废话
温柔的废话 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条回答
  •  执笔经年
    2020-11-27 10:22

    Regardless of the version, for your example, the is:

    {  $set: { lastLookedAt: Date.now() / 1000 }  }
    

    However, depending on your version of MongoDB, the query will look different. Regardless of version, the key is that the empty condition {} will match any document. In the Mongo shell, or with any MongoDB client:

    $version >= 3.2:

    db.foo.updateMany( {},  )
    
    • {} is the condition (the empty condition matches any document)

    3.2 > $version >= 2.2:

    db.foo.update( {}, , { multi: true } )
    
    • {} is the condition (the empty condition matches any document)
    • {multi: true} is the "update multiple documents" option

    $version < 2.2:

    db.foo.update( {}, , false, true )
    
    • {} is the condition (the empty condition matches any document)
    • false is for the "upsert" parameter
    • true is for the "multi" parameter (update multiple records)

提交回复
热议问题