請我試圖包裝我的頭如何動態查詢集合。我有一個具有以下模式的集合。如何構造動態MongoDb查詢
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
}
});
我創建了一個用戶界面,用戶可以使用可用字段的任意組合查詢數據。用戶可以使用sale_lease
字段,amount.amount
字段進行查詢搜索屬性,還可以查詢general_features
字段,該字段是一個數組。我不知道如何根據用戶選擇的偏好生成此查詢。我知道如何執行查詢,瞭解事先查詢的字段,但是使其動態變化是問題所在。
我想使用多個if
語句可能對所有可能的字段組合進行查詢,但我有點意識到,這不是實現這一目的的方式。如果我能指出正確的方向,我將非常感激。
不,不,請您詳細說明。由於 –
對不起,看錯問題 – chridam
見https://stackoverflow.com/questions/31995166/mongoose-optional-search-query-parameters – JohnnyHK