我必須編寫一個查詢,作爲查找位置的自動完成。 else語句在尋找合適的國家方面效果良好,但是當用戶編寫「德國,柏林」時,該算法拋出了其他可能擁有「柏林」城市的國家。我需要它只搜索位於例如「德國」的城市。有兩個條件的貓鼬正則表達式搜索
只是爲了清楚標誌變量在搜索參數字符串中有一個逗號時變爲true。然後它在國家和城市分裂。
if(flag){
World.findOne().or([{name:{$regex:country}},{cities: {$regex:city}}]).exec(function (err, users) {
if (err) return console.error(err);
console.log(users);
res.send(users);
});
}else{
World.findOne({name:{$regex:val}}, {cities: {$slice: 10}}).exec(function (err, users) {
if (err) return console.error(err);
console.log(users);
res.send(users);
});
}
我想這很好,但它拋出一個編譯錯誤:
if(flag){
World.findOne({{country:{$regex:country}},{cities: {$regex:city}}}).exec(function (err, users) {
if (err) return console.error(err);
console.log(users);
res.send(users);
});
嘗試同樣在findOne()
請告訴我的錯誤你好嗎? – abdulbarik
關於第二個代碼,我得到了 World.findOne({{country:{$ regex:country}},{cities:{$ regex:city}}})。ex ^ SyntaxError:意外的標記{ –