0

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.

3
  • No it doesn't please can you expatiate on it. Thanks Commented Sep 7, 2016 at 14:47
  • Sorry, misread the question Commented Sep 7, 2016 at 14:51
  • See stackoverflow.com/questions/31995166/… Commented Sep 7, 2016 at 15:37

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Michel Floyd your suggestion works but i am having a little trouble querying for a key whose value is an object. Like using my schema location.state. Please can you update your answer to reflect that so that i can accept it as the answer.
0

Without knowing the specifics of how users are able to make a selection for the query, the name of the collection, etc, it's difficult to provide a detailed example; however, you should be able to take the user's selection and turn it into a query parameter object. For example, assuming the collection name is Tripart_Property, if the user makes a selection for the sale_lease field, you could do the following:

var saleLeaseSelection = argumentContainingSelectionFromUser;
var customQuery = { sale_lease: saleLeaseSelection };
Tripart_Property.find(customQuery);

This is a very basic example. argumentContainingSelectionFromUser is the data from the user selection. You would probably want to build a method on the server that you could call from the client that would return the results from the query so that you could display them to the user, but this should at least get you pointed in the right direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.