在 mongodb 中将值从一个键复制到另一个键?

Copy Value from One key to another in mongodb?

我正在尝试将 mongodb 中的数据从 旧模式 迁移到 mongodb 中的 新模式 。其中添加了 slug_url,它是键值 'name' 的精确副本。我如何使用 mongoshell 实现此目的?

以前的数据:

{
name:'test',
}

想要

{
name:'test',
slug_url:'test'
}

可以使用 forEach -

db.<collectionName>.find().forEach(function(result) 
{ 
db.<collectionName>.update({"_id" : result._id}, {$set : {"slug_url" : result.name}}); 
})