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