MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Aggiornare un documento secondario in mongodb?

Specificando la posizione effettiva del documento incorporato in questo modo:

// update _id field of first author    
collection.update({'_id': "4f44af6a024342300e000001"}, 
                  {$set: { 'authors.0._id': "1" }} )

Oppure tramite operatore posizionale :

// update _id field of first matched by _id author    
collection.update({'_id': "4f44af6a024342300e000001",
                    //you should specify query for embedded document
                    'authors._id' : "4f44af6a024342300e000002" }, 
     // you can update only one nested document matched by query                   
                    {$set: { 'authors.$._id': "1" }} )