最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

搜索mongoose的多查询

运维笔记admin12浏览0评论

搜索mongoose的多查询

搜索mongoose的多查询

我想写一个提前查询来搜索mongoose使用nodejs,

search.js(nodejs)的代码

let queryOptions = {};

if(req.body.title){
    queryOptions.title = {$regex:key ,$options:"i"};
}   
if(req.body.name){
    queryOptions.name = {$regex:key ,$options:"i"};
}
if(req.body.tags){
    queryOptions.tags = {$regex:key ,$options:"i"};
}

Room.find({$or: [queryOptions]}, (err,rooms)=>{})

表单数据就像这样

key:"hello"
name:false
tags:false
title:true

当检查两个属性时,mongoose返回空结果

key:"hello"
name:true
tags:false
title:true
回答如下:

这是因为你的查询错了

使用这个

let queryOptions = [];

if(req.body.title){
    queryOptions.push({title: {$regex:key ,$options:"i"}})
}   
// ...

Room.find({$or: queryOptions}, (err,rooms)=>{})

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论