【问题标题】:How to change attribute name of the embed type?如何更改嵌入类型的属性名称?
【发布时间】:2020-10-29 20:37:13
【问题描述】:

如何通过查询 $rename 更改嵌入类型的名称?

{ list_name: [{ name_1: String }] } => { list_name: [{ name_2: String }] }

我试过db.getCollection('test').updateMany({}, { $rename: { 'list_name.name_1': 'name_2' }})

但它抛出一个错误:WriteError: cannot use the part (list_name of list_name.name_1) to traverse the element ({list_name: [ { name_1: "test" } ]})

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    如果这些字段在数组元素中,$rename 不起作用

    Refer

    要实现,您需要$unset$set 一个文档一个文档。你也可以使用bulkwrite

    list_name 是一个数组。因此$rename 不起作用。

    当我搜索时,我得到this。如果您的文档数量较少,您可以参考第二个答案。

    否则,您需要使用bulk writemongo dump options

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2017-01-31
      • 2017-06-24
      相关资源
      最近更新 更多