最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

搜索mongoose的多查询

IT培训 admin 6浏览 0评论

搜索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)=>{})

搜索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. 暂无评论