通过 spring 更改 mongo 文档的 ID

Change mongo document's id via spring

有没有办法修改 Mongo 集合中所有文档的 ID spring mongo?

尝试了很多方法后,我收到如下错误消息:

the (immutable) field '_id' was found to have been altered to _id

Write errors: [BulkWriteError{index=0, code=16837, message='The _id field cannot be changed from {_id: "xyz"} to {_id: "zxy"}.

有办法吗?

不,您不能更新 _id 字段。但是您始终可以为现有文档设置一个全新的 _id 并删除旧的 _id。