Olivine-Labs / resty-mongol

Lua MongoDB driver
MIT License
46 stars 23 forks source link

Using $regex in col:find() ? #9

Closed sunao-uehara closed 8 years ago

sunao-uehara commented 8 years ago

Is there any way to use $regex search with col:find() or col:find_one() ?

mongo cli supports regex search as below.

db.collection.find( { "name" : { $regex : /Andrew/i } } );

But it doesn't work, if I do like this with resty-mongol's col:find_one().

col:find_one({name = {$regex: /Andrew/i }})
DorianGray commented 8 years ago

Yes, you can. your code should look something like...

col:find_one({name = {['$regex'] = 'Andrew', ['$options'] = 'i'}})

mongo-cli uses javascript, which has a regex type. Lua does not. Since JSON doesn't either, mongo's wire protocol represents regexes in the form above instead of the more terse /Andrew/i format.

...now that I think about it, I believe this format is documented in https://docs.mongodb.org/v3.0/reference/operator/query/regex/`

sunao-uehara commented 8 years ago

Thanks!! It works perfectly.