Mahmoud Niypoo
Mahmoud Niypoo

Reputation: 1710

multi query for search mongoose

I want to write advance query to search in mongoose use nodejs,

code at 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)=>{})

the form data Like this

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

mongoose return empty result when checked two property like this

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

Upvotes: 0

Views: 254

Answers (1)

Gor
Gor

Reputation: 2908

it is because your query is wrong

Use this one

let queryOptions = [];

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

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

Upvotes: 1

Related Questions