mongodb将多个文档的字段类型更改为数组
我想在一个查询中更改多个文档的字段类型。
这是一些示例数据。第一个示例不可扩展,如果 person1 有多个 thing1
怎么办?
这:
{ "name": "person1", "things": { "thing1": { "thingInstanceID": 400 } } }
应该是:
{ "name": "person1", "things": { "thing1": [{ "thingInstanceID": 400 }] } }
我需要对数百个文档执行此操作,并且不仅仅是 thing1
,还有诸如 thing2
、thing3
、< code>thing4 等。我该怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于将“thing1”转换为数组,这里有一个选项:
解释:
在更新操作中使用聚合管道(mongodb 4.2+)将字段替换为数组
游乐场
For "thing1" to convert to array here is an option:
Explained:
Replace the field with array using aggregation pipeline( mongodb 4.2+ ) in the update operation
playground