2016-09-07 53 views
0

請我試圖包裝我的頭如何動態查詢集合。我有一個具有以下模式的集合。如何構造動態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語句可能對所有可能的字段組合進行查詢,但我有點意識到,這不是實現這一目的的方式。如果我能指出正確的方向,我將非常感激。

+0

不,不,請您詳細說明。由於 –

+0

對不起,看錯問題 – chridam

+0

見https://stackoverflow.com/questions/31995166/mongoose-optional-search-query-parameters – JohnnyHK

回答

0

讓我們在您的收藏有幾個鍵簡單的情況下,用戶可以對它們的任意組合查詢,並且要和結果。一個典型的模式是:

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); 

爲簡單起見,上面假定您已經給每個搜索字段一個ID等於對應的模式字段。您的命名約定可能會有所不同。

正如你所看到的,你開始一個空白查詢對象,然後看看每個搜索欄,如果它是一個非空,那麼你一鍵添加到與您要搜索的架構鍵查詢。

藉助良好的命名約定和簡單的字段結構,您甚至可以在js循環中執行此操作,因此每個密鑰不需要一個if語句。

+0

感謝@Michel弗洛伊德你的建議的作品,但我有一個小麻煩查詢其關鍵價值是一個對象。就像使用我的模式location.state一樣。請你可以更新你的答案以反映這一點,以便我可以接受它作爲答案。 –

0

如果不知道用戶如何能夠做出的查詢選擇的具體情況,收集等的名稱,就很難提供一個詳細的例子;但是,您應該能夠接受用戶的選擇並將其轉換爲查詢參數對象。例如,假設集合名稱是Tripart_Property,如果用戶作出對sale_lease領域的選擇,你可以做到以下幾點:

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

這是一個非常簡單的例子。 argumentContainingSelectionFromUser是來自用戶選擇的數據。你可能會想建立的是,你可以從會從查詢返回的結果,這樣你可以將其顯示給用戶的客戶端調用服務器上的方法,但至少應該讓你在正確的方向。