Mongoose: Regex: Error: Can't use $options with String

Created on 7 Nov 2011  路  15Comments  路  Source: Automattic/mongoose

As defined in the MongoDB documentation, the following syntax :

find(name: { $regex : 'abc', $options: 'i' } )

is correct.

However, I've got the error _Can't use $options with String_

The same query works when I use directly the mongo shell.
I'm using mongoose 2.3.10 and mongo 2.0

Most helpful comment

I'm having an almost identical issue, except that I'm using a variable:

x = 'abc'
find({name: { $regex : x, $options: 'i' }} )

and getting the same error. I had to solve it with:

find({ name: new RegExp(x, 'i')}})

All 15 comments

just pass a RegExp directly:

find(name: { /abc/i } )

I know, but since this syntax is valid in mongo, I would expect it to work in mongoose. What do you think ?

There is actually a caveat here.

I noticed when doing a simple query, aaron's example works and the normal mongodb setup does not.

However, when doing a complex query, it does work such as with $and and $or joins.

@jamescarr thats probably a bug. It sounds like we might be missing casting for those type of queries?

Maybe. For reference here is what I am using in my coffeescript based app (simplified to avoid confusion):

    [first, last] = "James Carr".split ' '
    searchOptions = {status:'new'}
    searchOptions.$and = [
        {'name.first': {$regex:first, $options:'i'}}
      , {'name.last': {$regex:last, $options:'i'}}
    ]

    Lead.find(searchOptions).desc('arrival').run (err, docs) ->
      cb err, docs

I'm having an almost identical issue, except that I'm using a variable:

x = 'abc'
find({name: { $regex : x, $options: 'i' }} )

and getting the same error. I had to solve it with:

find({ name: new RegExp(x, 'i')}})

+1 for this

I'm using $.ajax to send a JSON to a nodejs server with connect bodyParser middleware.

But it's very hard/impossible to serialize a RegExp to JSON to send over a POST request.

    $.ajax({
      url: "/db/find/Users/"
      dataType: 'json',
      type: 'post',
      data: JSON.stringify({where : {name : /example/i} }),
      contentType: 'application/json; charset=utf-8',
      async : false,
      success: function(data){
      }
    });

The problem here is if I JSON.stringify a RegExp it returns an empty object.
Having the ability to use $options in this scenario is very welcome.

JSON.stringify({where : {name : { $regex : "example", $options : "i"} } }) 

it's in master. need to backport to 2x

On May 11, 2012, at 3:22 AM, [email protected] wrote:

+1 for this

I'm using $.ajax to send a JSON to a nodejs server with connect bodyParser middleware.

But it's very hard/impossible to serialize a RegExp to JSON to send over a POST request.

   $.ajax({
     url: "/db/find/Users/"
     dataType: 'json',
     type: 'post',
     data: JSON.stringify({where : {name : /example/i} }),
     contentType: 'application/json; charset=utf-8',
     async : false,
     success: function(data){
     }
   });

The problem here is if I JSON.stringify a RegExp it returns an empty object.
Having the ability to use $options in this scenario is very welcome.

JSON.stringify({where : {name : { $regex : "example", $options : "i"} } }) 

Reply to this email directly or view it on GitHub:
https://github.com/LearnBoost/mongoose/issues/598#issuecomment-5647784

Another consideration is that Mongo supports PCRE allowing for things like negative look-behinds where javascript does not. Passing strings allows us to reconcile these differences.

Actually this has long-since been fixed. Should work fine @jwerre

I'm having the same issue. mkpolas workaround worked for me. The only thing I'm not getting is that the

{"$regex", "something", "$options": "i"}

works in one script and in the other one it doesn't even though they are more or less identical.

@relief-melone can you craft a complete example with the query you expect to work but isn't working? You can share it here, or create a new issue, or feel free to join us on either Slack or Gitter.im to chat about it.

content := param
query:=bson.M{"name " :bson.M{"$regex":content , "$options": "$i"}}

@relief-melone {"$regex", "something", "$options": "$i"}
it worked for me in my code, Hope it can help you

Also i find it it doesn't work well if there are some special characters in your query,like Brackets锛孮uotation mark锛宎nd so on .
So use regexp.QuoteMeta(Your content) will work.

Was this page helpful?
0 / 5 - 0 ratings