0

So, I'm using mongodb and trying to do a search for a substring in a key, if any document contains the substring it should return the document withing the query.

This is what im using:

db.events.find({name: '\.*substring\.'})

But it only matches some names, not every name that contains the substring...

I've tried using the $text and $search operator, but it seems that it only queries if I input the whole name, not the substring.

1 Answers1

2

Try:

db.events.find({"name": {"$regex": ".*substring*.", "$options": "i"}}))

Use $option i if you wanted case insensitive. You have all the options available as per mongo $regex, here. Or just remove altogether.

  • thanks, yours actually worked but different, cause yours is supposed to begin with the string, i used .*substring*. to match any that contains the substring... thanks!! – Matheus Rezende Feb 14 '18 at 18:10
  • 1
    @MatheusRezende I’ve updated my answer –  Feb 14 '18 at 18:38