I am a beginner in mongodb so would be great if someone advice me in how to write the below query efficiently.
I have a collection which has location and date as fields
There are 4 conditions for search
- User can search without any parameter
- User can search with date alone
- User can search with location alone
- user can search with both date and location
db.collection.find(query) --> Here this query object will change based on the request parameter.
- If no req param ,
query = {} - If location present ,
query = {location query} - If date present ,
query = {datequery} - If both date and location
query = {location query , datequery}
How i can make it simple with mongodb commands ? I know how to do it in SQL by adding whereclause based on the non null params in request but getting confused with nosql syntax.