Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
How to modify the "key" element in MongoDB document?

I am having the below document structure

[
  {
    "network_type": "ex",
    "rack": [
      {
        "xxxx": {
          "asn": 111111,
          "nodes": {
            "business": [
              "sk550abcc1eb01.abc.com",
              "sk550abcc1eb10.abc.com",
              "sk550abcc1eb19.abc.com",
              "sk550abcc1eb28.abc.com"
            ]
          },
          "region": "ex-01",
          "zone": "01a"
        }
      }
    ]
  }
]

I need to rename/update the key array element "xxxx" to "details".

I tried the below command , but it doesn't seem to work.

db.collection.update({},
{
  $rename: {
    "rack.xxxx": "details"
  }
})

LINK : https://mongoplayground.net/p/9dcDP-VKZ55

Please help me.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can't direct $rename the field name which is within the array.

Instead,

  1. Iterate with document(s) in the rank array, create the details field with the value of xxxx and next append this field to each document.

  2. Remove the path with $rank.xxxx to remove the xxxx field from the document(s) in the rank array.

db.collection.update({},
[
  {
    $set: {
      rack: {
        $map: {
          input: "$rack",
          in: {
            $mergeObjects: [
              {
                "details": "$$this.xxxx"
              },
              "$$this"
            ]
          }
        }
      }
    }
  },
  {
    $unset: "rack.xxxx"
  }
])

Sample Mongo Playground

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda