Mongoose or request

I need to find documents based on several text fields.

var term = new RegExp(req.query.search, 'i'); .find({ company_name: { $regex: term }}); 

Using the above, works great. However, when I try to add an extra field using

 .find(...).or([{ bio: { $regex: term }}]); 

he cannot get any records. I need to do this for a few more fields.

There are various text fields in the schema that are indexed, but not together as a single multi-pole index. Will this work better, and if so, are there any clear examples of this? The documentation I found was quite rare.

Any ideas?

+6
source share
1 answer

Put both fields in the array that is passed to or :

 .find().or([{ company_name: { $regex: term }}, { bio: { $regex: term }}]); 

Separate single-field indexes are what you want, since each or term is executed as a separate query.

+9
source

Source: https://habr.com/ru/post/954885/


All Articles