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

¿Cómo actualizar el _id de un documento MongoDB?

No puedes actualizarlo. Tendrás que guardar el documento usando un nuevo _id y luego elimine el documento antiguo.

// store the document in a variable
doc = db.clients.findOne({_id: ObjectId("4cc45467c55f4d2d2a000002")})

// set a new _id on the document
doc._id = ObjectId("4c8a331bda76c559ef000004")

// insert the document, using the new _id
db.clients.insert(doc)

// remove the document with the old _id
db.clients.remove({_id: ObjectId("4cc45467c55f4d2d2a000002")})