D
D
d1zz72021-05-25 17:38:58
Mongoose
d1zz7, 2021-05-25 17:38:58

How to search with regular expressions in mongoose?

OK. I have, for example:
const { searchText } = req.body;
And you need to execute But use as in SQL - LIKE '% %'. It seems to have found how, like find({name: /Value/}), but it does not work. model.find({ name: searchText });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Romanov, 2021-05-26
@d1zz7

Search using regular expressions: Works slowly, if there is an index on the field it will be faster, but only if it is the beginning of a word /^Value/ It is better to use a text index for searching use: Text Index $text
find({name: {$regex: /Значение/}}),
createIndex( { name: "text" } )
find( { $text: { $search: "Значение" } } )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question