mongoose-datatables icon indicating copy to clipboard operation
mongoose-datatables copied to clipboard

Search not working if find contains $or condition, and search contains more than one field

Open victormunoz opened this issue 6 years ago • 0 comments

For example, this options parameter does not work:

var options={
        find:{ $or:[ {'status':0}, {'status':2} ]},
        search: {
            value: "hello",
            fields: ['name','city']
        }
    };

It seems the searching fields conditions are added to the $or of the find, but I think this is not the expected behaviour. Instead, it should create an $and element containing two $or (one for the find and another one for the search fields), like this:

var options={
        find:{ 
            $and:[ 
                 {$or:[ {'status':0}, {'status':2} ] },
                 {$or:[ {'name':{'$regex': 'hello', '$options': 'i'}}, {'city':{'$regex': 'hello', '$options': 'i'}} ] }
            }
        }
    };

I think an "else" is needed in the "if" of the line 37 of mongoose-datatables.js

victormunoz avatar Jan 28 '19 11:01 victormunoz