【发布时间】:2018-02-05 14:31:48
【问题描述】:
我正在使用 nodejs、express 和 mongodb 开发一个 Web 应用程序。我需要一个搜索栏,在我输入时从 mongo 数据库中的集合中为我提供建议。我正在搜索的是在this web page 中实现的搜索栏。如何实现?
【问题讨论】:
标签: node.js mongodb express search
我正在使用 nodejs、express 和 mongodb 开发一个 Web 应用程序。我需要一个搜索栏,在我输入时从 mongo 数据库中的集合中为我提供建议。我正在搜索的是在this web page 中实现的搜索栏。如何实现?
【问题讨论】:
标签: node.js mongodb express search
对于一个简单的实现,只需向您的服务器发送一个包含搜索关键字的请求,例如:“mobile”
然后在 mongo 中,使用正则表达式定位想要的字段,然后返回结果。
正面:
// on input change
$.ajax({
method: "GET",
url: "http://searchuri",
data: { search: mysearchinput }
})
.fail(function(err) {
console.log(err.responseJSON);
})
.done(function(data) {
// do stg with your datas
});
返回:
Datas.find({ productname: { $regex : ".*"+ req.query.search +".*", $options:'i' } }, function(err, result){
return res.status(200).json({result: result})
});
【讨论】: