gpt4 book ai didi

mongodb - 如何重新映射 mongodb 集合的字段?

转载 作者:可可西里 更新时间:2023-11-01 10:25:40 25 4
gpt4 key购买 nike

想象一下——我有一个集合,它有一个特定的映射。

假装:

{
"field": "lala",
"subdoc": {
"otherfield": "la",
"etcfield": "la"
}
}

我需要将其转换为不同的数据结构:

{
"field-gets-renamed": "lala",
"subdoc-has-different-stuff-in-it": {
"something": "la",
"something-else": "la"
}
}

这样做的典型方法是什么? (有吗?)

我是否逐个文档读取 collection1 并将其写入不同的集合?或者有没有办法只重新映射 collection1 中的字段?

我非常感谢一些例子,因为我是 mongodb 的新手。

最佳答案

您应该使用 $rename operator

你的情况是:

db.collectionName.update({}, { $rename : { 'field' : 'field123',  'subdoc' : 'subdoc123', 'subdoc.otherfield' : 'subdoc.otherfield123', 'subdoc.etcfield' : 'subdoc.etcfield123'} }, false, true);

P.S: 我没有测试过上面的命令。

关于mongodb - 如何重新映射 mongodb 集合的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com