Reputation: 75
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?
Upvotes: 1
Views: 4515
Reputation: 4678
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})
});
Upvotes: 1