Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!