Update by ObjectId

I am a MongoDB newbie and the examples I am seeing is using other fields as a filter but I want to update using the ObjectId
How do you update by ObjectId in mongosh?
I tried
db.devices.updateOne({"_id": ObjectId("6427f848b39a9135e7b7e63e")}, {"$set" :{"value": "on"}})

but no rows were updated.

What should be the correct query in this case?

The code is good.

You might be connected on the wrong server.

You might be using the wrong database.

You might be using the wrong collection.

You might be using a non-existing _id on the correct collection of the correct database on the correct server.

I don’t think your code is correct!?


db.devices.updateOne({"_id": ObjectId("6427f848b39a9135e7b7e63e")}, {"$set" :{"value": "on"}})



You have " " around: _id, $set, and value. Try running your code with out the quotes you made originally.

db.devices.updateOne({_id: ObjectId("6427f848b39a9135e7b7e63e")}, {$set :{value: "on"}})

Like mentioned

There is no problem with the quotes. In fact the quotes are required with the dot notation.

mongosh> db.devices.find()


{ _id: ObjectId("6427f848b39a9135e7b7e63e"), value: 'initial' }


mongosh> db.devices.updateOne({"_id": ObjectId("6427f848b39a9135e7b7e63e")}, {"$set" :{"value": "on"}})

{ acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0 }

mongosh> db.devices.find()

{ _id: ObjectId("6427f848b39a9135e7b7e63e"), value: 'on' }