话不多说,直接上代码
db.collection_name.find().forEach(
function(item){
db.collection_name.update({_id:item._id},{$set:{update_time:item.create_time}})
}
)
类似于js的写法,一个一个替换 相当于MySQL的
update collection_name set update_time=create_time
本文档展示了如何使用JavaScript在MongoDB中通过`find()`遍历并逐一更新每个文档的`update_time`字段,将其设置为创建时间,实现类似SQL的批量替换操作。

被折叠的 条评论
为什么被折叠?



