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

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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