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
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.
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.
Most helpful comment
I'm having an almost identical issue, except that I'm using a variable:
and getting the same error. I had to solve it with: