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

156
Visualizações
how to monogo aggregate and make certain changes to its output

I have some documents like this and we need certain changes to its output

"gameId": "string",
"markets": {

 "market X": {
    "instagram": "string",
    "lastVersion": 0,
    "market": "market X",
    "name": "string",
    "supportedVersion": 0,
    "telegram": "string",
    "config":{"A":1,"B":2 , "C":"c" }

  },
  "market Z":{
    "instagram": "string",
    "lastVersion": 0,
    "market": "market Z",
    "name": "string",
    "supportedVersion": 0,
    "telegram": "string",
    "config":{"D":5,"E":8 , "F":"vb" }
  }
}

and I want write a monogo aggregate make the output as follows

[
  {
    "gameId": "string",
    "instagram": "string",
    "lastVersion": 0,
    "market": "string",
    "name": "string",
    "supportedVersion": 0,
    "telegram": "string"
  },
  {
    "gameId": "string",
    "instagram": "string",
    "lastVersion": 0,
    "market": "string",
    "name": "string",
    "supportedVersion": 0,
    "telegram": "string"
  }
]

I tried to get the right answer but it didn't work What do you think I should do?

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

0

This aggregation pipeline produces your desired output from your example document.

db.collection.aggregate([
  {
    "$addFields": {
      "x": {
        "$map": {
          "input": {
            "$objectToArray": "$markets"
          },
          "in": "$$this.v"
        }
      }
    }
  },
  {
    "$unset": "x.config"
  },
  {
    "$addFields": {
      "x.gameId": "$gameId"
    }
  },
  {
    "$unwind": "$x"
  },
  {
    "$replaceWith": "$x"
  }
])

Try it on mongoplayground.net.

over 4 years ago · Santiago Trujillo Relatório

0

I try to get correct answer by this query

game_collection.aggregate([
            {
                "$match": {
                    "_id": ObjectId(data["gameId"])
                }
            },
            {
                "$project": {
                    "x": {"$objectToArray": "$markets"},

                }
            },
            {
                "$replaceRoot": {
                    "newRoot": {
                        "gameId": data["gameId"],
                        # "name": "$x.k",
                        "address": "$x.v"
                    }
                }
            },
            {
               "$addFields": {
               "gameId": f"$$this.v.gameId",
                }
            }
         ])
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