2017-03-04 40 views
0

我有一個基本的領域,其中包括興趣,位置座標 我需要用一個特定的用戶ID進行POST請求到得到結果的用戶模式查詢/查找MongoDB的文件與一系列的多個條件

app.post('api/users/search/:id',function(err,docs) 
 
    { //find all the documents whose search is enabled. 
 
     //on documents returned in above find the documents who have atleast 3 common interests(req.body.interests) with the user with ':id' 
 
       // -----OR----- 
 
     //find the documents who stay within 'req.body.distance' compared to location of user with':id' 
 
     
 
     //Something like this 
 
     return User 
 
    .find({isBuddyEnabled:true}), 
 
    .find({"interests":{"$all":req.body.interests}}), 
 
    .find({"_id":req.params.id},geoLib.distance([[req.body.gcordinates],[])) 
 
    });

基本上我需要執行發現裏面找到或內部查詢查詢..

回答

0

按照代碼中的你的意見,你要使用多個條件在你的find查詢中,這些條件中的任何一個都滿足並返回基於它的結果。您可以使用$or$and來實現它。下面給出條件類似於您的示例代碼。

find({ 
    $or:[ 
     { isBuddyEnabled:true }, 
     { "interests": { "$all":req.body.interests }}, 
     { $and:[ 
       { "_id":req.params.id }, 
       { geoLib.distance...rest_of_the_condition } 
       ] 
     } 
     ] 
});