Lobosan
Lobosan

Reputation: 135

MeteorJS dynamic query

Is it possible to build queries dynamically? For instance, I need to build a function like this:

var dynamicQuery = function(collectionName) { return collectionName.find({}); }

Upvotes: 0

Views: 96

Answers (1)

David Weldon
David Weldon

Reputation: 64342

You have two options:

pass the collection itself

var dynamicQuery = function(Collection) {
  return Collection.find();
};

dynamicQuery(Posts);

pass the name of the collection

var dynamicQuery = function(name) {
  var root = Meteor.isClient ? window : global;
  var Collection = root[name];
  return Collection.find();
};

dynamicQuery('Posts');

Recommended reading: collections by reference.

Upvotes: 1

Related Questions