从数组中为集合的所有文档重命名字段-MongoDB

时间:2020-01-10 17:27:00

标签: mongodb mongodb-query

我有一个如下文件

[
  {
    "_id" : NUUID("51611712-b966-4562-8937-06015a6691ec"),
    "Name":"Alex",
    "Descriptions" : [ 
        {
            "Name" : "Descr1",
            "Items" : [ 
                {
                    "ItemsType" : {
                        "_id" : 1,
                        "Name" : ""
                    },
                    "Field1" : 1,
                    "Field2" : null
                }, 
                {
                    "ItemsType" : {
                        "_id" : 2,
                        "Name" : ""
                    },
                    "Field1" : 1,
                    "Field2" : null
                }, 
                {
                    "ItemsType" : {
                        "_id" : 3,
                        "Name" : ""
                    },
                    "Field1" : 1,
                    "Field2" : null
                }
            ]
        }
    ],
    "DateCreated" : ISODate("2019-07-29T11:33:19.090Z"),
    "DateModified" : ISODate("2019-12-23T08:08:40.339Z"),
    "IsDeleted" : false,
    "LegalEntityIdentifier" : "",
  }, 
  ...
]

对于集合的所有文档,如何将具有路径“ Descriptions.Items.ItemType”的数组字段重命名为新名称“ Descriptions.Items.Type”?

1 个答案:

答案 0 :(得分:3)

您需要使用True运算符

$map

MongoPlayground