Vasu Rangpariya
Vasu Rangpariya

Reputation: 57

how to apply regex find in mongodb

{
 "category" : {
   "100" : {
      "location" : "class.xyz.20"
    }
  }
}

How can I direct Apply regex on location key field in MongoDB Like

{"location" : {'$regex' : "class."}}

how can I Do That

Upvotes: 1

Views: 78

Answers (1)

Roshan Raj
Roshan Raj

Reputation: 198

I have used regex in aggregate pipelines, You can try it out on find too (not sure on the syntax for find you can check the docs for it).

Here is how I used regEx in Aggregate.

db.collection.aggregate([
    { $match: { "field": { $regex: "someText", $options: 'i'} } }
]);

You can read more about options here

Upvotes: 1

Related Questions