如何使用数据库命令重命名数组中的字段? [英] How to rename a field inside an array with database commands?
问题描述
免责声明:尽管相似,但与MongoDB 重命名数组内的数据库字段.
Disclaimer: Even though similar, not the same as MongoDB rename database field within array.
我想使用 数据库命令 而不是 shell.我发现使用 $rename 是不可能的数组内.所以我转向 $set 和 $unset 并创建了这个:
I want to change the key of a field from name
to title
with database commands and NOT the shell. I found out that using $rename is not possible inside an array. So I turned to $set and $unset and created this:
db.runCommand({
update: 'apps',
updates: [
{
q: {versions: {$elemMatch: {title: {$exists: false}}}},
u: {
$set: {'versions.$.title': '$name'}
},
multi: true
},
{
q: {versions: {$elemMatch: {name: {$exists: true}}}},
u: {
$unset: {'versions.$.name': ''}
},
multi: true
}
]
})
这有点作用.它完成了我想要的一切,但它将每个标题/名称更改为字符串$name";而不是字段 name
的值.
This works somewhat. It does all I want but it changes every title/name to the string "$name" instead of the value of the field name
.
我也试过这个:
db.runCommand({
update: 'apps',
updates: [
{
q: {versions: {$elemMatch: {title: {$exists: false}}}},
u: {
$set: {
versions: {
$map: {
input: "versions",
as: 'each',
in: {
"title": "$$each.name"
}
}
}
}
},
multi: true
},
{
q: {versions: {$elemMatch: {name: {$exists: true}}}},
u: {
$unset: {'versions.$.name': ''}
},
multi: true
}
]
})
但这只会导致 ['versions.$map' 中的美元 ($) 前缀字段 '$map' 对存储无效.]
.我将如何重命名数组中的字段?
But that just results in [The dollar ($) prefixed field '$map' in 'versions.$map' is not valid for storage.]
. How would I go about renaming a field inside an array?
以下是我的应用程序集的缩小版本以供参考.我真的只想将每个名为 name
的键更改为 title
.
Below is a minified version of my apps collection for reference. I literally only want to change every key called name
into title
.
[{
identifier: "x",
versions: [
{
name: "test_name"
version: "x.x.x"
},
{
name: "test_name"
version: "x.x.x"
},
]
},
{
identifier: "y",
versions: [
{
name: "test_name2"
version: "x.x.x"
},
{
name: "test_name2"
version: "x.x.x"
},
]
}, ... ]
推荐答案
但这只会导致
'versions.$map' 中以美元 ($) 为前缀的字段 '$map' 对存储无效.
.
- 错误说
$map
的input
接受使用$
符号的引用字段$version
, - 将
u
对象括在数组括号中,用于 使用聚合管道更新 - 只需将字段
title
和version
放在$map
中 $unset
不是必需的,因为$map
将用in
中的新字段替换旧数据
- The error says
$map
'sinput
accepts reference field using$
sign$version
, - enclose the
u
object in array bracket for update with an aggregation pipeline - just put both field
title
andversion
in$map
$unset
is not required because$map
will replace old data with new fields inin
db.runCommand({
update: 'apps',
updates: [
{
q: { "versions.name": { $exists: true } },
u: [{
$set: {
versions: {
$map: {
input: "$versions",
in: {
"title": "$$this.name",
"version": "$$this.version"
}
}
}
}
}],
multi: true
}
]
})
第二种方式,更动态的方法
Second way, For more dynamic approach
$mergeObjects
在$map
内,防止手动列出键值对$unset
阶段从version
数组中删除name
字段
$mergeObjects
inside$map
, to prevent manual list of key-values pair$unset
stage to removename
field fromversion
array
db.runCommand({
update: 'apps',
updates: [
{
q: { "versions.name": { $exists: true } },
u: [
{
$set: {
versions: {
$map: {
input: "$versions",
in: {
$mergeObjects: [
"$$this",
{ "title": "$$this.name" }
]
}
}
}
}
},
{ $unset: "versions.name" }
],
multi: true
}
]
})
这篇关于如何使用数据库命令重命名数组中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!