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

295
Vistas
How to combine MongoDB Collections that don't have reference to each other?

Hi I have 2 collections that don't have reference to each other, I want to merge both of them in one collection, then I do pagination.

However it's not working, I only get result when page is 1 but for any other pages I just get an empty array

const search = async (skip, limit, query) => {

  const regex = new RegExp(`^${query}`, 'i')

  const cursor = await db.collection('cats').aggregate([
    { $unionWith: { 
        coll: 'dogs', 
        pipeline: [{ $project: { 'breeds': false } }]
      }
    },
    { $match: { $or: [ 
        { 'dog_name': { $regex: regex } }, 
        {'cat_name': { $regex: regex } }
    ]}},
    { $limit: limit },
    { $skip: skip } 
  ]).toArray()
  
  return cursor
}

const result1 = search(0, 10, 'Mi') // This works

const result2 = search(10, 10, 'Mi') // This doesn't page(2)

const result2 = search(20, 10, 'Mi') // This doesn't page(3)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this:

const cursor = await db.collection('cats').aggregate([
    { $unionWith: { 
        coll: 'dogs', 
        pipeline: [{ $project: { 'breeds': false } }]
      }
    },
    { $match: { $or: [ 
        { 'dog_name': { $regex: regex } }, 
        {'cat_name': { $regex: regex } }
    ]}},
    { $skip: skip } ,
    { $limit: limit }
    
  ]).toArray()

You are limiting before skipping, maybe that's the issue.

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