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

259
Visualizações
how to create a key value pair from fields in mongodb?

i have some data structured like this :

        "location_identifiers": [
            {
                "permalink": "olivette-missouri",
                "uuid": "e1774b1c-634d-4ea4-1414-cd8be15df631",
                "location_type": "city",
                "entity_def_id": "location",
                "value": "Olivette"
            },
            {
                "permalink": "missouri-united-states",
                "uuid": "51a065b8-05d5-1a28-3fcd-1ad143f1f725",
                "location_type": "region",
                "entity_def_id": "location",
                "value": "Missouri"
            }

i want to restructure it to look like this, i am using mongodb compass:

"location_identifiers": [
            {
                "city": "Olivette",
                "region": "Missouri"
            }
]

i have tried unwind and project query but i am stuck, any help would be greatly appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use $arrayToObject

db.collection.aggregate([
  {
    "$match": {}
  },
  {
    "$set": {
      "location_identifiers": [
        {
          "$arrayToObject": {
            "$map": {
              "input": "$location_identifiers",
              "as": "item",
              "in": {
                k: "$$item.location_type",
                v: "$$item.value"
              }
            }
          }
        }
      ]
    }
  }
])

mongoplayground

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