I'm relatively new to Mongo & mongoose, and I've hit a problem.
I have a reasonably (for me anyway) complex query, that will allow the user to search for all entered terms.
so if the query is something like so:
var query = { '$and' : [
{ "foo1" : "bar1" },
{ '$and' : [ "foor2" : { $ne : null } }, { "foo2" : "bar2" } ] },
{ "foo3" : "bar3" }
]};
Doc.find(query);
but the user can enter any number of combinations for the parameters, i.e. I could search for all items that match foo1 & foo2, or just all items that match foo2, or just foo3, etc.
Is there a way to tell the query to only look for a parameter if it isn't empty, or is there a way to build searches like this programmatically? I have seen other options, for adding parameters like this, but they only seem to add in the standard
{ foo : 'bar' }
format, and for some reason they always seem to get added to query whether they meet the conditions of the if statement or not.
Thanks.