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

319
Vistas
How to merge two collections in mongodb, aggregate mongodb

I have 2 collections: configs and points The points collection has a name field that stores the id for the configs collection

How can two collations be combined, given that the collection has an Array in which the objects in which to search.

configs:

{
    "find": true,
    "points": [{
        "image": true,
        "brand": true,
        "_id": {
            "$oid": "61f0ad909535d94c98056e11"
        },
        "name": "ABC"
    }, {
        "image": true,
        "brand": true,
        "_id": {
            "$oid": "61f0ade99535d94c98056e17"
        },
        "name": "Odido"
    }]
}

points

{
    "name": "61f0ad909535d94c98056e11",
    "userID": "61c49f94a02ff82b484cc6d0",
    enter code here
    "create_date": {
        "$date": "2022-01-27T02:53:52.473Z"
    }
}

I would like to get this result, well, or something similar

{
    "name": "61f0ad909535d94c98056e11",
    "userID": "61c49f94a02ff82b484cc6d0",
    "config_name": {
        "image": true,
        "brand": true,
        "_id": {
            "$oid": "61f0ad909535d94c98056e11"
        },
        "name": "ABC"
    },
    "create_date": {
        "$date": "2022-01-27T02:53:52.473Z"
    }
}

i am trying to use query like this

    Points.aggregate([
          {
            $lookup: {
              from: "configs",
              localField: "name",
              foreignField: "_id",
              as: "configs",
            },
          },
        ]);

Thanks in advance for your help :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

db.points.aggregate([
  {
    $match: { "name": "61f0ad909535d94c98056e11" }
  },
  {
    $set: { "name": { "$toObjectId": "$name" } }
  },
  {
    $lookup: {
      from: "configs",
      localField: "name",
      foreignField: "points._id",
      as: "config_name"
    }
  },
  {
    $set: {
      "config_name": {
        "$first": {
          "$filter": {
            "input": { "$first": "$config_name.points" },
            "as": "item",
            "cond": { "$eq": [ "$name", "$$item._id" ] }
          }
        }
      }
    }
  }
])

mongoplayground

about 4 years ago · Juan Pablo Isaza 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