How to generate AND or OR mongo queries with multiple values?


how do I generate an OR() or AND() mongo query from an object where each item has a value to search for and a boolean stating if it should be added to the query?

The main question here, is: How can I generate a mongo query instruction by adding several ANDs or ORs, without knowing in advance how many I will have?

The use case: A file browser like list in which we select filtering keywords.




not quite sure what the problem is from what you are saying

but you can just build the query using the appropriate conditional logic on whether you want to add conditions.

var query = {}

query["$and"] = []

var firstOr = []
firstOr.push({blah: true});
firstOr.push({awesome: false});
query["$and"].push({"$or": firstOr})


Thanks. Bottom of the line, it is an interpreted language from whatever string.
I just suppose, I need to take care of adding “(” and “)” too where needed in your example?