akram mouha akram mouha - 1 month ago 17
Javascript Question

Multiple search and filter Meteor & Mongodb

I have simple collection :

nom: {
type: String,

},
prenom:{
type: String,

},

categorie:{
type: String,

},
age:{
type:String,

} ,

ville : {

type : String

}


I want to find easy way to multiple search and filter elements into a table. For example filter by name and age, or filter by age and city, or filter by all of them. Here is my table :
my table

In my case what's the best way to make this multiple filter on mongodb and meteor without using the searchengine package.

Answer

This is how I tend to do my searches, so that it also finds similar results as well. So if there is a letter "A" in my result, if I search on "A" it will bring up everything with the the letter "A" until I get more specific.

Note you might want to make the search value and search keys to lowercase Because "A" != "a" and will not return anything.

I also put an int value you just incase that question came up.

let collection = yourCollection.find({
       "nom": {
            $regex: ".*CONDITION_VALUE.*"
       },
       "prenom": {
            $regex: ".*CONDITION_VALUE.*"
        },
        "categorie": {
            $regex: ".*CONDITION_VALUE.*"
        },
        "age": {
            $gt: minAge,
            $lt: maxAge
        },
        "ville": {
            $regex: ".*CONDITION_VALUE.*"
        }
 });