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

232
Views
How to correctly execute asynchronous calls to mongodb in javascript/typescript?

I have an operation kinda like so:

const results = Promise.all([
    myColl1.find(query1).sort(sort).limit(limit).toArray(),
    myColl2.find(query2).sort(sort).limit(limit).toArray(),
]); 

However, it seems the results are completely unexpected. After looking what might possibly going on, the documentation states:

Warning: Because asynchronous calls directly modify the cursor, executing asynchronous calls on a single cursor simultaneously can also cause undefined behavior. Always wait for the previous asynchronous operation to complete before running another.

The "single cursor" part seems to imply it might be possible to do this with two separate cursors? I'm actually not too sure of the technical details but, wouldn't the fact that myColl1 != myColl2 mean they're separate cursors? I've actually been considering joining the collections, so if they were the same, would it be possible to use separate cursors? Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Do you use await? I made some changes to what you originally posted:

const [res1, res2] = await Promise.all([
  await myColl1.find(query1).sort(sort).limit(limit).toArray(),
  await myColl2.find(query2).sort(sort).limit(limit).toArray()
]);

I hope this helps ✌🏼

about 4 years ago · Juan Pablo Isaza 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!