0

I'm trying to create easy search engine and I'm wondering if there is a way to create conditions in query, something like :

Meteor.users.find({
   if(gender_given) gender:"something",
   if(name_given) name:"something"
});

Right now all that comes to my mind is to create variable with whole Meteor.users.find().fetch() collection, and then loop through whole array looking for matching records, but I'm sure this is not going to be effective.

Am I missing something?

Community
  • 1
  • 1
sdooo
  • 1,851
  • 13
  • 20

2 Answers2

2

You can build a dynamic selector to accomplish this. For example:

var selector = {};

if (gender)
  selector.gender = gender;

if (name)
  selector.name = name;

Meteor.users.find(selector);
David Weldon
  • 63,632
  • 11
  • 148
  • 146
0

Why not:

var result;
if (gender_given) {
  result = Meteor.users.find({
    gender:"something"
  });
} else if (name_given) {
  result = Meteor.users.find({
    name:"something"
  });
}
stubailo
  • 6,077
  • 1
  • 26
  • 32
  • Because I want it to have multiple searching options, and not necessary all have to be given, so with 3 options there are 9 ifs, with 4 16 ifs etc. – sdooo Oct 31 '14 at 18:53
  • Yeah the other answer is better, go with that one – stubailo Oct 31 '14 at 18:56