zoukankan      html  css  js  c++  java
  • mongoose模糊查询

    模糊查询要用到 $or 和 $regex

    前端请求:

    search(){
            let searchV=$.trim($("#searchV").val());
            if(searchV.length<1){
              alert("请输入关键字进行搜索");
              return;
            }
            $.ajax({
              url:"http://localhost:3000/search/"+searchV,
              success:res=>{
                console.log("查询的数据",res)
                if(res.data.length>0){
                  this.isSearch=true
                  this.searchValue=res.data
                }
              },
              error:err=>{
                console.log(err)
              }
            })
    
          }

    express后端代码:

    // 根据模糊信息查询简讯
    app.get("/search/:searchV",(req,res)=>{
        let regexp=new RegExp(req.params.searchV,'i')
        
        randomArt.find({$or:[{title:{$regex:regexp}},{content:{$regex:regexp}},{author:{$regex:regexp}}]},(err,doc)=>{
            if(err){
                console.log(err)
                res.send({
                    code:400,
                    msg:"查询失败"
                })
            }
            if(doc){
                res.send({
                    code:200,
                    msg:"查询成功",
                    data:doc
                })
            }
        })
    })
  • 相关阅读:
    51串口通信
    juicer使用备忘
    51单片机音乐盒程序
    最精简24L01程序--接收
    sqlserver 数据库迁移
    sqlserver自增主键
    js keycode
    tabindex 去掉虚线
    div 绑定keyup
    sqlserver 当前时间减去30天
  • 原文地址:https://www.cnblogs.com/shanchui/p/13066076.html
Copyright © 2011-2022 走看看