I'm developing a web application using nodejs, express and mongodb. I need a search bar giving me suggestions from a collection in the mongo database while I'm typing. What I'm searching for is the search bar implemented in this web page . How can I implement it?
For a simple implementation, just send a request to your server containing the search keyword, example : "mobile"
Then in mongo, target the fields wanted with a regex then return the result.
Front:
// 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
});
Back:
Datas.find({ productname: { $regex : ".*"+ req.query.search +".*", $options:'i' } }, function(err, result){
return res.status(200).json({result: result})
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.