MeteorJS dynamic query

97 views Asked by At

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

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

1

There are 1 answers

1
David Weldon On BEST ANSWER

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.