【问题标题】:Update Embedded Document Mongo Field Name更新嵌入文档 Mongo 字段名称
【发布时间】:2020-05-25 17:29:46
【问题描述】:

我浏览了这个链接:How to rename a document field in a MongoDB?,但它对我来说不能正常工作。我有如下的 Mongo 文档。我正在使用MongoDB server version: 4.0.3。

{
    "_id" : ObjectId("5cb825e566135255e0bf38a4"),
    "firstName" : "John",
    "lastName" : "svc_user",
    .....
    .....
    "status" : "A",
    "effDate" : ISODate("2012-08-24T01:46:33.000Z"),
    "department" : [ 
        {
            "deptName" : "KG",
            ....
            ....
        }, 
        ...
        ....
        .....
    ],
    ...
    ...
    ...
}

我执行了以下查询:

db.employee.update({}, {$rename:{"department.deptName":"department.departmentName"}}, false, true);

错误: 不能使用part(department of department.deptName)遍历元素

【问题讨论】:

标签: mongodb


【解决方案1】:

在 Mongo 4.2 中你可以运行这个:

db.collection.updateMany(
   {
      department: { $exists: true },
      "department.deptName": { $exists: true }
   },
   [{
      $set: {
         department: {
            $map: {
               input: "$department",
               in: { departmentName: "$$this.deptName" }
            }
         }
      }
   }]
)

我假设在 4.0.3 中。您必须使用 JavaScript 中的循环逐一修改字段名称

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多