meteor - How To Structure Dynamic MongoDb Query -


please trying wrap head on how query collection dynamically. have collection has below schema.

tripart_property_schema = new simpleschema({     "type" : {         type : string,         label : 'property type',         },      "sale_lease" : {         type : string,         label : '',      },      "location" : {         type : object,         label : '',          optional : true     },      "location.state" : {         type : string,         label : 'state',         },      "location.lga" : {         type : string,         label : 'lga',         optional : true      },      "location.address" : {         type : string,         label : 'address',         optional : true      },      "amount" : {         type : object,         label : '',         optional : true     },      "amount.amount" : {         type : number,         label : '',         optional : true     },      "amount.discount" : {         type : number,         label : '',         optional : true     },      "active" : {         type : boolean,         label : '',         optional : true     },      "views" : {         type : number,         label : '',         optional : true     },      "date_added" : {         type : date ,         label : '',         optional : true     },      "general_features" : {         type : [string],         label : '',         optional : true     },      "outdoor_features" : {         type : [string],         label : '',         optional : true     },      "indoor_features" : {         type : [string],         label : '',         optional : true     },      "other_facilities" : {         type : object,         label : '',         optional : true      },      "other_facilities.bedroom" : {         type : number,         label : '',         optional : true      },      "other_facilities.bathroom" : {         type : number,         label : ' ',         optional : true      },      "other_facilities.garage" : {         type : number,         label : '',         optional : true      },      "other_facilities.staffquaters" : {         type : number,         label : '',         optional : true      } }); 

i have created user interface user can query data using combination of available fields. user can make query searching property using sale_lease field, amount.amount field , query general_features field array. don't know how generate query based on user selected preference. know how perform query knowing fields queried before hand, making dynamic problem lies.

i trying make use of multiple if statements possibly make query possible combination of fields, kind of realize not way achieve this. grateful if can pointed right direction.

let's take simple case collection has several keys, user can query on combination of them, , want , results. typical pattern be:

let query = {}' if ( $("#type").length ) query.type = $("#type").val(); if ( $("#sale_lease").length ) query.sale_lease = $("#sale_lease").val(); if ( $("#state").length ) query.location = { state: $("#state").val() }; return tripart_property.find(query); 

for simplicity, above assumes you've given each search field id equal corresponding schema field. naming convention may vary.

as can see, start blank query object @ each search field, if non-blank add key query corresponding schema key want search on.

with naming conventions , simple field structure can in js loop don't need 1 if statement per key.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -