1

Is there a search all filter when trying to search through the db? I have multiple select boxes that I would like to get the value of and then according to the values, it searches for the data. I found that I would need to have an if statement for each select box to check if it is the default value(String) or a selected value(Integer).

items.find({
        "person.age": obj.age, //is 'All'
        "person.gender": obj.gender // is 'male'
      },{
        limit: this.state.limit
      }).fetch()

In my example above, one is the default (all) and the other is the selected value. When I try to search, it would return nothing because of the 'all'. I am looking for a symbol or something to replace the 'all'.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
user8813240
  • 125
  • 1
  • 7
  • are you looking for something like $or operator? – Ankit Apr 08 '18 at 07:48
  • 2
    How about get all of the select values, "filter out" the ones with the default ("all") values that do not constrain the query, and build a query filter from the remaining values? – MasterAM Apr 08 '18 at 09:32
  • I believe your desired solution is described here: https://stackoverflow.com/questions/30935591/meteor-and-with-or – Harry Adel Apr 08 '18 at 11:52

1 Answers1

2

@masteram is right - you want to avoid the keys that contain All - there is no * in mongodb. You can simplify your logic at the same time.

The following will copy only the keys of obj that are not equal to All then run the search on those keys:

const person = {};
Object.keys(obj).forEach((k) => {
  if (obj[k] !== 'All') person[k] = obj[k];
});

items.find(person, { limit: this.state.limit }).fetch()
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39