How to run mongo queries in parallel?

Suppose in a method I have

let data1 = collection1.find(...).fetch();
let data2 = collection2.find(...).fetch();

Is there a way to run the two queries in parallel while preserving the meteor environment?

I believe the easiest way to do parallel tasks is to use Promise.awaitAll.

Here’s a quick example from something we do in our app:

import { Promise } from 'meteor/promise'

let myArray = [obj1, obj2];
Promise.awaitAll( (myObj) {
  // use myObj somehow.

Basically just regular Promise.all with async/await built in. (Although for your use-case, perhaps you don’t even need the awaitAll version.)

1 Like