There is a document

{ id: 10, orders: [ { id: 1 name: order1, files: [ {id: 1, name: file1} ] }, { id: 2 name: order2, files: [ {id: 1, name: file1}, {id: 2, name: file2} ] } ] } 

You need to change the value of "name" in the file id: 1, the application id: 2. You only need to change the name value without overwriting the object of the file or application, or especially the entire document. After all, if another user makes a change in the document or order id: 2, then my update will overwrite them.

How to solve this problem?

  • specify a specific path that needs to be updated - Dmytryk
  • orders [1] .files [0] .name - Alexey

1 answer 1

db.collection.updateOne({id:10}, {$set:{'orders.1.files.0.name': 'qwerty'}})