mongodb - 更新嵌入式文档 Mongo 字段名称
问题描述
我浏览了这个链接:如何在 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);
错误:无法使用部分(department.deptName的部门)遍历元素
解决方案
在 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 中的循环一一修改字段名称
推荐阅读
- reactjs - React slick how to enable no pause
- javascript - 即使我已经传递了另一个引用,数组仍然以某种方式遵循引用
- excel - 有没有办法检查参数是否可以迭代
- flask - 检查烧瓶中数组中的无(神社模板)
- python - 为什么 render / requests-html 不抓取动态内容?
- c - 为什么这个说法是错误的?有人可以解释
- javascript - Javascript类:公共字段和字段有什么区别
- laravel - Laravel 用 excel 导出
- django - CustomUser登录视图应该如何实现
- javascript - 未找到 Nano 画廊“jQuery(...).nanogallery2 不是函数”