tc / RMongo

R client to interface with MongoDB
102 stars 34 forks source link

Can RMongo do regex query? #8

Closed defoo closed 13 years ago

defoo commented 13 years ago

Hi,

I am trying to query my mongodb with a regex query such as the one below:

mongo <- mongoDbConnect("test_database") output <- dbInsertDocument(mongo, "test_data", '{"foo": "bar"}') dbGetQuery(mongo, 'test_data', '{"foo": "/bar/i"}')

However, RMongo seems to treat it as a simple string matching, and returns nothing: data frame with 0 columns and 0 rows

I then tried another way to specify the regex:

dbGetQuery(mongo, 'test_data', '{"foo": { "$regex" :"/bar/i" } }')

And this time, the following is returned:

Error in .jcall(rmongo.object@javaMongo, "S", "dbGetQuery", collection, : java.lang.NullPointerException

I am wondering if there is a way to specify regex in RMongo?

Thanks, Derek

tc commented 13 years ago

Try this mongodb regex convention: {"foo": {"$regex": "bar", "$options": "i"}}

see http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON for more details.

defoo commented 13 years ago

Thanks. This works perfectly!