搜索栏,其中包含关于nodejs和mongodb的建议 [英] Search bar with suggestions with nodejs and mongodb

查看:45
本文介绍了搜索栏,其中包含关于nodejs和mongodb的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs,express和mongodb开发Web应用程序.输入时,我需要一个搜索栏,向我提供mongo数据库中某个集合的建议.我要搜索的是在此网页中实现的搜索栏.我该如何实施?

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?

推荐答案

对于简单的实现,只需将包含搜索关键字的请求发送到您的服务器,例如:"mobile"

For a simple implementation, just send a request to your server containing the search keyword, example : "mobile"

然后在mongo中,使用正则表达式定位所需的字段,然后返回结果.

Then in mongo, target the fields wanted with a regex then return the result.

正面:

// 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})

  });

这篇关于搜索栏,其中包含关于nodejs和mongodb的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆