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

89
Vistas
Search by multiple aggregate lookup functions in MongoDB

I'm really struggling with how to be able to search via both Song and Artist name. Each are in their own collections and are connected via an ID. I have written the aggregate successfully, however the search term only filters via the Song name. Is there a way I can make this search by both song name and artist name. The second field I want to search by is the artistName on the project object. As an example of the new search that doesn't work - "Bust A Move Young MC", with "Young MC being" the artist name. In the current state, this simply returns 0 results. Any help would be much appreciated.

      const searchTerm = 'Bust A Move'
  let filter = []

  const s = new RegExp(searchTerm.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'), 'gi')
  filter = ['name'].map((field) => ({
    [field]: s
  }))

  const agg = await Song.aggregate([
    {
      $lookup: {
        from: 'artists',
        localField: 'artist_id',
        foreignField: 'artist_id',
        as: 'artist'
      }
    },
    { $unwind: '$artist' },
    {
      $match: {
        $or: filter
      }
    },
    {
      $project: {
        _id: 1,
        name: 1,
        song_id: 1,
        spotifyImg640: 1,
        artist_id: '$artist.artist_id',
        artistName: {
          name: '$artist.name'
        }
      }
    }
  ])

// Song Object
{
song_id: 1
name: "Bust A Move",
artist_id: 1
}

// Artist Object
{
artist_id: 1,
name: "Young MC"
}
about 4 years ago · Juan Pablo Isaza
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