sql >> Base de Datos >  >> NoSQL >> MongoDB

MongoDB:Subdocumento de actualización

Necesitas usar el operador posicional $

Por ejemplo:

update({ 
       _id: 7, 
       "comments._id": ObjectId("4da4e7d1590295d4eb81c0c7")
   },{
       $set: {"comments.$.type": abc}
   }, false, true
);

No lo probé, pero espero que te sea útil.

Si desea cambiar la estructura del documento, debe usar

db.collection.update(criterios,objNuevo, upsert, multi)

Argumentos:

criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, nsert it
multi - if all documents matching criteria should be updated

e inserte nuevo objNew con nueva estructura. revisa esto para más detalles