【问题标题】:Search bar with suggestions with nodejs and mongodb带有 nodejs 和 mongodb 建议的搜索栏
【发布时间】:2018-02-05 14:31:48
【问题描述】:

我正在使用 nodejs、express 和 mongodb 开发一个 Web 应用程序。我需要一个搜索栏,在我输入时从 mongo 数据库中的集合中为我提供建议。我正在搜索的是在this web page 中实现的搜索栏。如何实现?

【问题讨论】:

    标签: node.js mongodb express search


    【解决方案1】:

    对于一个简单的实现,只需向您的服务器发送一个包含搜索关键字的请求,例如:“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})
    
      });
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 2013-03-13
      • 2019-03-17
      • 2011-11-10
      相关资源
      最近更新 更多