Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

389
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda