Loren
Loren

Reputation: 14856

How do I perform database queries in parallel in Meteor?

Collection:

docs:
  _id
  name

Code:

names = Docs.findOne(id).name for id in doc_ids

But I would like the findOne queries to be sent off in parallel.

Upvotes: 0

Views: 112

Answers (2)

Tomasz Lenarcik
Tomasz Lenarcik

Reputation: 4880

You can restrict your query to a certain subsets of fields, e.g.

names = _.pluck(Docs.find({_id:{$in:doc_ids}}, {
  fields: {name:1}
}).fetch(), 'name');

Upvotes: 1

David Weldon
David Weldon

Reputation: 64312

A possibly easier alternative is to use $in and do the parallel query on the database itself. Here's an example in CoffeeScript:

ids = ['abc123', 'def456', 'hij789']
names = (doc.name for doc in Docs.find({_id: $in: ids}, {fields: name: 1}).fetch())

Upvotes: 2

Related Questions