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?

Ashh
  • 44,693
  • 14
  • 105
  • 132
Sithys
  • 3,655
  • 8
  • 32
  • 67
  • 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. – Neil Lunn May 13 '18 at 21:46

1 Answers1

5

Try to use $regex with $options

Customer.find({'name': { $regex: 'FOO', $options: 'i' }}).exec(function (err, customer) {
     console.log(customer);
})
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • 1
    i was just writing the comment, that it doesn't work, but with your edit, now everything works like a charme - big thanks! – Sithys May 13 '18 at 18:43