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

197
Vistas
Add new field to each array elements in Mongodb aggregation based on match condition

I have the following mongodb collection

 {  "id" : "yzes547",
    "courseId" : "aw4a34y7",
    "objectives" : [ 
        {
            "type" : "LESSON",
            "id" : "dfhxytd"
        }, 
        {
            "type" : "MODULE",
            "id" : "rgzeerp"
        }
    ]
}

I want to add new fields for ids of each type in objectives parameter.

Desired output

{  "id" : "yzes547",
    "courseId" : "aw4a34y7",
    "objectives" : [ 
        {
            "type" : "LESSON",
            "id" : "dfhxytd"
        }, 
        {
            "type" : "MODULE",
            "id" : "rgzeerp"
        }
    ]
   "lessonId": "dfhxytd",
   "moduleId": "rgzeerp"
}

I have tried the following query but it only returns False.

db.account.aggregate([
{
    '$project': {
        '_id': 0, 
        'questionId': 1, 
        'courseId': 1, 
        'lessonId': {
            '$cond': [
                {
                    '$eq': [
                        '$objectives.type', 'LESSON'
                    ]
                }, '$objectives.id', False
            ]
        }, 
        'moduleId': {
            '$cond': [
                {
                    '$eq': [
                        '$objectives.type', 'MODULE'
                    ]
                }, '$objectives.id', False
            ]
        }

Is it possible to query the database such that we can get new fields of each type in one aggregation method?

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

0

Yes, you can achieve this quite easily using $map and $objectToArray to create the required fields, finally by using $replaceRoot and $mergeObjects you can add these fields to the original structure, like so:

db.collection.aggregate([
  {
    $replaceRoot: {
      newRoot: {
        "$mergeObjects": [
          "$$ROOT",
          {
            "$arrayToObject": {
              $map: {
                input: "$objectives",
                as: "obj",
                in: {
                  k: {
                    $concat: [
                      {
                        "$toLower": "$$obj.type"
                      },
                      "Id"
                    ]
                  },
                  v: "$$obj.id"
                }
              }
            }
          }
        ]
      }
    }
  }
])

Mongo Playground

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