- Błąd mówi
$mapinputakceptuje pole referencyjne za pomocą$podpisz$version, - załącz
uobiekt w nawiasie tablicy dla aktualizacji z potok agregacji - po prostu wstaw oba pola
titleiversionw$map $unsetnie jest wymagane, ponieważ$mapzastąpi stare dane nowymi polami win
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
}
]
})
Drugi sposób, dla bardziej dynamicznego podejścia
$mergeObjectswewnątrz$map, aby zapobiec ręcznej liście par klucz-wartość$unsetetap do usunięcianamepole zversiontablica
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
}
]
})