4

I'd like to search my database for Customers that start with FOO or have FOO in their names. For that i took a look at this question -> How to query MongoDB with "like"?

After that i build my query so that it looks like this ->

Customer.find({'name': '/FOO/'}).exec(function (err, customer) {
     console.log(customer);
})

but although there is a customer with FOO inside the 'name' i get no result. If i modify my query to ...({'name': 'FOO'})... instead of ...({'name': '/FOO/'})... i get my customer. What am i doing wrong?

1
  • You should have looked more carefully. The usage is right there within the answers and of course various links to the $regex documentation. Also not the "only" question on the subject anyway. Commented May 13, 2018 at 21:46

1 Answer 1

5

Try to use $regex with $options

Customer.find({'name': { $regex: 'FOO', $options: 'i' }}).exec(function (err, customer) {
     console.log(customer);
})
Sign up to request clarification or add additional context in comments.

1 Comment

i was just writing the comment, that it doesn't work, but with your edit, now everything works like a charme - big thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.