I am developing a web app using Codeigniter and MongoDB.
I need to create a function that is searching the database using the following criterias.
- Tags
- Owner username
- Status
- Date range
It needs to be flexible enough for users to for example only search by tags or status and leave out the other criterias.
Right now I am using if else structure with different queries that reflect the chosen
criterias. For example the below code. This is very bad way because then I need to create
a query matching every possible combination.
if ($args['status']) {
$this->mongo_db->select ($select)->
where('user_name', $args['username']) ->
where ('status', $args['status']) ->
where_in_all ('file_tags', $tags) ->
limit ($limit) ->
offset ($offset) ->
get ('files');
} else {
$this->mongo_db->select ($select)->
where('user_name', $args['username']) ->
where_in_all ('file_tags', $tags) ->
limit ($limit) ->
offset ($offset) ->
get ('files');
}
Is there not a better way of doing search in MongoDB?
Why don’t you want to create query based on user output and use it later to get data from mongoDb. Below is pseudo-code