Mongodb Node.js正则表达式搜索 [英] Mongodb Nodejs Regex Search

查看:64
本文介绍了Mongodb Node.js正则表达式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在制作一个网站,作为部分搜索表格.参考来源: https://www.youtube.com/watch?v=ZC2aRON3fWw& t = 42s 但是我不明白为什么它不起作用.我用哈巴狗代替了hbs.

Hey I am making a website which as a partial search form. Reference from:https://www.youtube.com/watch?v=ZC2aRON3fWw&t=42s But I couldnt understand why it doesnt work. I use pug instead of hbs.

这些是我的代码:

    app.get('/sonuc', function(req, res, next){
  var q = req.query.q;
  Article.find({
    title : {
      $regex: new RegExp(q)
    }
  }, {
    _id:0,
    __v:0
    }, function(err, data){
      res.render('sonuc', {data:data})
    }).limit(10);
  });
});

然后这是我的布局哈巴狗:

Then this is my layout pug:

.ui-widget
      form.form-inline.my-2.my-lg-0
        input.form-control.mr-sm-2(type='text', onkeyup='showResults(this.value)', placeholder='Search',action='/article/'+sorgu, aria-label='Search')
        button.btn.btn-secondary.my-2.my-sm-0(type='submit')
        #search-results(style='width:60px;top:-1px;position:relative')

在这种布局的哈巴狗中,我的问题是onkeyup问题不起作用.如何在该表格上实现该功能?

In this layout pug I thing the onkeyup issue is not working. How can I implement that fu nction on that form?

这是我的main.js,它从数据库中获取查询并以html形式编写:

And ths is my main.js whihc takes query from database and write it in html form:

    var showResults = debounce(function(arg){
  var value = arg.trim();
  if(value == "" || value.length <= o){
    $("#search-results").fadOut();
    return;
  }else{
    $("#search-results").fadeIn();
  };
  var jqhr = $.get('/article/' + value, function(data){
  })
  .done(function(data){
    if(data.length == 0){
      $("search-resuts").append('<p classs="lead text-center mt-2">No Results</p>');
  }else{
    data.forEach(x => {
      $("search-resuts").append('<a href="#"><p class="m-2 lead"><img style="width:60px;" src="images/supreme1.jpg">' + x.title +'</p></a>');
    });
  }
  })
  .fail(function(err){
    console.log(err);
  })
}); 200;
function debounce(func, wait, immediate){
  var timeout;
  return function(){
    var context = this;
    args = arguments;
    var later = function(){
      timeout= null;
      if(!immediate)func.apply(context,args);
    };
    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if(callNow)func.apply(context,args);
    };
  };

我无法理解这些问题以及为什么它不起作用.总之,我想制作一个可与regex和mongodb一起使用的搜索引擎.在引用的我的文章上方的youtoube视频中将显示部分内容.但是真正的问题是,我无法理解showResults函数的代码块,而且我不知道如何将这些代码转换为我的项目.这样我就在等待您的帮助.我无法将视频上传到该网站,因此,如果您可以给我您的Facebook,Instagram或电子邮件帐户,我可以发送应解决的问题.我需要你的帮助.我为学校做了很长时间的这个项目,但我不能继续前进.拜托我需要你的帮忙.希望我能很好地表达自己,您的帮助会解决这个问题.

I cannot understand these issues and why it doesnt work.As a summary, I want to make a search engine which works with regex and mongodb. İt will be partial that is shown in that youtoube video which is on the above of my article that I referenced. But the real issue is, I couldnt understand the code block of function showResults and I dont know how to translate this codes to my project. So that I am waiting your help. I cannot upload a video to this website so that if you can give me your facebook, instagram or email account I can send the issue which should be solved. I need your help. I have been making this project for a long time for my school but I cannot move on. Please I need your help. I hope I could express myself well and your helps will solve it.

推荐答案

是的,我知道.我做了很多更改,而不是使用此代码块.我不知所措: https://www.youtube.com/watch?v=9_lKMTXVk64 而且我还记录了:使用Mongodb进行模糊搜索?这两个文档对我有很大帮助.因此,我的代码几乎与这些文档中所示的代码相同90%.这是我的app.js:

Yes I got it know. I have made lots of changes instead of using this code blockes. I wathced : https://www.youtube.com/watch?v=9_lKMTXVk64 And also I documented : Fuzzy Searching with Mongodb? These two documentations help me a lot. So that my code is almost approximately 90% same as shown in these documentations. This is my app.js :

app.get('/sonuc', function(req, res){
  if (req.query.search) {
    const regex = new RegExp(escapeRegex(req.query.search), 'gi');
    Article.find({ "title": regex }, function(err, articles) {
        if(err) {
            console.log(err);
        } else {
           res.render("index", { 
             articles: articles 
            });
        }
    });
  }
});

这是我的index.pug文件:

This is my index.pug file :

extends layout

block content
  body
    br 
    br
    br
    ul.list-group
      each article, i in articles
        li.list-group-item
          a(href="/article/" + article._id)= article.title

这是我的layout.pug文件(但我会考虑使用文本格式)

And this is my layout.pug file(But I will consider the text form)

  form.form-inline.my-2.my-lg-0(action="/sonuc" method="GET")
    input.form-control.mr-sm-2(type='text', name,="search", onkeyup='showResults(this.value)', placeholder='Search',action='/article/'+sorgu, aria-label='Search')
    button.btn.btn-secondary.my-2.my-sm-0(type='submit')

请查看: https://github.com/nax3t/fuzzy-search

因为在我的解决方案中,我没有添加当我们找不到该单词时所应用的消息.或者,当没有这样的单词被用户搜索时.noMatch查询.但是,将其应用于我的项目之后,我将其添加到此处.

Because in my solution I didnt add the message that is applied when we cannot find the word. Or when there is no such a word which is searched by user. The noMatch query. But after I will apply it to my project I will add it to here.

这篇关于Mongodb Node.js正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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