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

388
Vistas
How to fetch the key value from an array in MongoDb?

I have the below document structure in MongoDB.

 [
  {
    "network_type": "ex",
    "rack": [
      {
        "xxxx": {
          "asn": 111111,
          "nodes": {
            "business": [
              "tt550abcc1eb01"
            ],
            "master": [
              "tt550abcc1eb02"
            ]
          },
          "region": "ex-01",
          "zone": "01a"
        },
        "yyyy": {
          "asn": 22222,
          "nodes": {
            "business": [
              "er44abcc1eb28"
            ]
          },
          "region": "ex-02",
          "zone": "02a"
        }
      }
    ]
  }
]

The names inside nodes array of business, master are unique. Is it possible to find the nodes array details by just passing the value "er44abcc1eb28" ? I tried the below query but it didn't work out. Please help.

     db.collection.find({"rack.$[].nodes.$[]": "er44abcc1eb28"},{})

https://mongoplayground.net/p/1yH_q0-9Iik

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

0

playground

db.collection.aggregate([
  {              //Reshape the array
    "$unwind": "$rack"
  },
  {
    "$project": {
      "racks": { //To handle dynamic key i.e xxxx, yyyy
        "$objectToArray": "$rack"
      }
    }
  },
  {.            //Reshape to match the element
    $unwind: "$racks"
  },
  {
    "$match": { //match condition
      "racks.v.nodes.business": "er44abcc1eb28"
    }
  },
  {
    $group: {   //Group the results incase there are more matches per id
      "_id": "$_id",
      data: {
        $push: "$racks"
      }
    }
  },
  {
    "$project": {
      "rack": { //Reshape the data to the original form
        "$arrayToObject": "$data"
      }
    }
  }
]) //To understand, remove each stage and check, refer the documentation.
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