Please i am trying to wrap my head over how to query a collection dynamically. I have a collection that has the 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 a user interface where a user can query the data using any combination of the available fields. A user can make a query searching for a property by using the sale_lease
field, amount.amount
field and also query the general_features
field which is an array. I don't know how to generate this query based on the user selected preference. I know how to perform the query knowing the fields queried before hand, but making it dynamic is where the problem lies.
I am trying to make use of multiple if
statements to possibly make a query for all possible combination of fields, but i kind of realize that this is not the way to achieve this. I will be very grateful if i can be pointed to the right direction.
Let's take the simple case where your collection has several keys, the user can query on any combination of them, and you want to AND the results. A typical pattern would 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, the above assumes that you've given each search field an ID equal to the corresponding schema field. Your naming convention may vary.
As you can see, you start with a blank query object then look at each search field, if it is non-blank then you add a key to the query corresponding to the schema key you want to search on.
With good naming conventions and a simple field structure you can even do this in a js loop so you don't need one if
statement per key.