BOU*_*lid 2 mongodb mongo-shell
我在 mongodb 中有一个文档结构,我想为我的所有文档更改它,而不使用像这个响应这样的聚合函数,而是通过创建一个特定的函数,并得到一个结果:
{
"_id" : ObjectId("String"),
"content" : {
"href" : "String",
"text" : "String",
"code" : "String "
}
}
Run Code Online (Sandbox Code Playgroud)
对此:
{
"_id" : ObjectId("String"),
"href" : "String",
"text" : "String",
"code" : "String "
}
Run Code Online (Sandbox Code Playgroud)
请提出任何建议。谢谢你。
最简单的方法是使用聚合框架。然后将输出保存到newCollection验证后,您可以删除旧的并重命名创建的。
db.collection.aggregate([{
$project : {
_id : 1,
href : "$content.href",
text : "$content.text",
code : "$content.code",
}
}, {
$out : "newCollection"
}
])
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢聚合(或者您content的 内容可能因文档而异),您也可以使用
db.coll.find().forEach(function (d) {
db.coll.update({
_id : d._id
}, {
$set : d.content,
$unset : {
'content' : 1
}
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2347 次 |
| 最近记录: |