Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda