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

254
Vistas
MongoDB aggregation - combine multiple values of a document from collection A to lookup a single value within a document from collection B

Say I have the following two collections, sites and webpages. I'm trying to understand how to create an aggregation that'll allow me to combine values of a document from the sites collection and use that to lookup a value from the webpages collection. In addition, I need to prepend the combined values with a string.

// sites collection
[
  { "_id" : 3, "host" : "www.example-foo.com", "path": "/bar", "hasVisited": false },
]
// webpages collection
[
  { "_id" : 5, "url" : "https://www.example-foo.com/bar" },
  { "_id" : 8, "url" : "https://www.fizz.com/buzz" },
]

Without an aggregation I would do something like the following.

const site = await db.sites.findOne({ hasVisited: { $eq: false } });

const pages = await db.webpages.find({
  url: `https://${site.host}${site.path}`, // <--- how to construct this in a lookup aggregation? string + value + value
});
// pages = [{ "_id" : 5, "url" : "https://www.example-foo.com/bar" }]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is like translation of your code with the 2 find queries in 1 using $lookup

Query

  • first findOne is the $match and the $limit 1
  • $set url is to make the string concat
  • second find is to do the $lookup (with the 1 site from above stages)

*if you want to do it for more than 1 sites remove the limit, and project more fields, to know where this pages belong to(which site)

Test code here

db.sites.aggregate([
  {
    "$match": {
      "hasVisited": {
        "$eq": false
      }
    }
  },
  {
    "$limit": 1
  },
  {
    "$set": {
      "url": {
        "$concat": [
          "https://",
          "$host",
          "$path"
        ]
      }
    }
  },
  {
    "$lookup": {
      "from": "webpages",
      "localField": "url",
      "foreignField": "url",
      "as": "pages"
    }
  },
  {
    "$project": {
      "_id": 0,
      "pages": 1
    }
  }
])
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