mongodbmeteorminimongo

Looking for symbol to filter all


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'.


Solution

  • @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()