2015-12-01 38 views
0

這是上一個問題的後續代碼。我想通過params,當有文本然後工作,當沒有文本,然後我沒有結果。如果沒有文本和日期,那麼查詢應該返回所有文件,其中date =該值。有條件的查詢,其中沒有要搜索的文本

var query; 

if(!req.query.text){ 
    console.log("no text") 
    query = Record.find({}) 
} else { 
    console.log("YES text") 
    query = Record.find(
     { $text : { $search : req.query.text } }, 
     { score : { $meta: "textScore" } } 
    ) 
} 




if(req.query.year){ 
    var years = req.query.year.split(','); 
    if(years.length < 2){ 
     query = query.where('date.year', req.query.year) 
    } else { 
     query = query.where('date.year').gt(years[0]).where('date.year').lt(years[1]); 
    } 
} 
if(req.query.loc === 1){ 
    query = query.exists('loc') 
} 
query 
    .select('title date.year') 
    .sort({ score : { $meta : 'textScore' } }) 
    .limit(50) 
    .exec(function(error, results) { 
     res.json(results) 
}); 

我的小白控制檯日誌記錄工作,因此,如果語句的工作,但查詢生成器似乎不喜歡我在做什麼。

回答

0

我解決了這個問題。這是這條線,沒有文字,沒有文字分數。簡單。

.sort({ score : { $meta : 'textScore' } })