php - User input for a MongoDB search -


i trying accept user input user may able search database.

> db.test2.find().pretty() {     "_id" : objectid("55de8a17f8389e208a1e7d7e"),     "name" : "john",     "favorites" : {         "vegetable" : "spinach",         "fruit" : "apple",     } } {     "_id" : objectid("55de8a17f8389e208a1f6gg4"),     "name" : "becky",     "favorites" : {         "vegetable" : "spinach",         "fruit" : "apple",     } } {     "_id" : objectid("55e3b6cbec2740181355b809"),     "name" : "liz",     "favorites" : {         "vegetable" : "spinach",         "fruit" : "banana",     } } 

in example, user able search combination of person's favorite vegetable, fruit, or both favorite vegetable , favorite fruit. if user entered spinach favorite vegetable, 3 returned. however, if user input favorite vegetable = spinach , favorite fruit = apple, john , becky returned.

in mongodb, able determine parameter want search. trying write code in way if user leaves field blank, should not searched for.

i have tried

$query = array("favorites.vegetable" => "$userinput", "favorites.fruit" => "$userinput2"); 

but if either of fields left blank, not return results. thought trying use if statements:

if ($vegetable == null) {     $query = array("favorites.fruit" => "$fruit"); }  else if($fruit == null) {     $query = array("favorites.vegetable" => "$vegetable"); }  else {     $query = array("favorites.vegetable" => "$vegetable", "favorites.fruit" => "$fruit"); } 

but if make database searchable more parameters have many conditional statements. there way make mongo search recognize when field left blank?

the question is, "where input coming from?". if have sort of structure input, coding quite simple follow pattern.

as is, 2 basic variables can clean code contruct query based on in variables:

$query = array();  if ( $fruit != null ) {     $query["favorites.fruit"] = $fruit; }  if ( $vegetable != null ) {     $query["favorites.vegetable"] = $vegetable; ) 

which means either end $query here either blank match or contains specific arguments ( either 1 or 2 ) depending on whether content null or not.

if input has structure, can lot more dynamic:

$input = array("fruit" => "apple", "vegetable" => "spinach");  $query = array();  foreach ( $input $key => $value ) {     $query["favorites.$key"] = $value; } 

which doing same thing appending $query in more dynamic way individual variables.

also note far mongodb concerned, document structure not great. should this:

{     "_id" : objectid("55de8a17f8389e208a1e7d7e"),     "name" : "john",     "favorites" : [         { "type": "vegetable", "name": "spinach" },         { "type": "fruit", "name": "apple" }     ] } 

and while may query comes out bit more complex, removing "specific paths" in query keys "vegetable" , "fruit" have whole lot of benefits make life lot easier, , primarilly "data" can "indexed". key names not indexable search , therefore loose efficiency:

$input = array("fruit" => "apple", "vegetable" => "spinach");  $query = array();  foreach ( $input $key => $value ) {   $query['$and'][] = array(     'favorites' => array(       '$elemmatch' => array(           'type' => $key, 'name' => $value       )     )   ); } 

which nice query using $elemmatch find documents contain "all" array elements "both" "type" , "name" of specified items in input list.

basically looks in json:

{   "$and": [     { "favorites": {       "$elemmatch": {           "type": "fruit", "name": "apple"       }     }},     { "favorites": {       "$elemmatch": {           "type": "vegetable", "name": "spinach"       }     }}   ] } 

it comes down knowing how manipulate data structures in chosen language, , "data structures" mongodb queries are, far language concerned.

as structure change condider finding people have "vegetables" in "favorites" becomes this:

{ "favorites.type": "vegetable" } 

which nice since "favorites.type" can indexed, opposed to:

{ "favorites.vegetable": { "$exists": true } } 

which while can "technically" use index, not in such nice way. changing way schema represented desirable , gives more flexibility.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -