node.js - 猫鼬部分搜索
问题描述
我正在尝试从集合字段 property_address 中搜索位置。它包含诸如 SouthEx、New delhi 之类的值。如果我搜索 southEx , New delhi,Delhi,India ,它将显示包含 southEx,New Delhi 的结果。
我们将如何使用猫鼬搜索来做到这一点?
解决方案
形式:
<form method="GET">
<input type="text" name="q" id="q"/>
</form>
对输入文本的 keyup 的 Ajax 请求
<script>
$(document).on('keyup','#q',function(){
if($(this).val()!=='' || $(this).val!=undefined){
var query=$(this).val();
$.ajax({
url:'http://localhost:3000/search?q='+ query,
type:'GET',
dataType:'json',
success:function(response){
console.log(response);
if(response.status=='ok'){
console.log(response.search_results);
}
},
error:function(){
alert('Failed to search address');
}
});
}
});
</script>
服务器.js
app.get('/search',function(req,res) {
if(req.query && req.query.q) {
var q = req.query.q;
YourModel.find({"property_address": new RegExp('^'+q+'$', "i")}, function(err, data) {
if(!err && data) {
return res.json({status:'ok',search_results:data});
}
});
} else {
return res.json({status:'error',message:'No input'});
}
});
推荐阅读
- android - 如何强制为 Android APP 重新加载数据
- python - Flask - base.html 中的引导程序
- attachment - PowerApps 附件控制如何检查是否已制作任何附件
- python - Python:将.txt文件读入数据框并写回文件后无法获取原始格式
- email - Prestashop 1.7 将新变量添加到电子邮件 total_shipping_tax_excl
- python - 通过在python中解析xml文件来获取父元素
- csv - 线图绘制奇怪;散点图拒绝时间戳
- vue.js - 无法读取 Vue 中未定义的属性 0
- django - PUT/更新外键和多对多关系仅与 pk,而不是整个对象
- mysql - 使用php在mysql表中添加一些值