Discussion:
com.mongodb.util.JSONParseException with forward slash for regex in JAVA
Ignatij Suvorov
2013-01-16 10:57:42 UTC
Permalink
Hello.

JSON.parse is throwing exception
DBObject dbObj = (DBObject)JSON.parse("{$match: { $and: [{errorCode: {$not:
/.*/}}]}}");

How to escape / characters?
Dwight Merriman
2013-01-17 15:55:35 UTC
Permalink
1) try quoting the left hand side names as shown below
2) i'm not sure if that function will do regular expressions. IIRC there
is no regular expression type in the JSON RFC, that is "javascript" json.
but BSON does support regular expression type so you can do it is just a
question of how i don't know the java driver well but it might take $regex
or you could use other means in the driver to make the BSON

http://docs.mongodb.org/manual/reference/mongodb-extended-json/
Post by Ignatij Suvorov
Hello.
JSON.parse is throwing exception
{$not: /.*/}}]}}");
How to escape / characters?
Thank you!
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Jeff Yemin
2013-01-19 04:21:49 UTC
Permalink
JSON.parse will handle unquoted key names, but not Javascript regular
expression. You have to use strict-mode extended JSON, as Dwight suggests:

DBObject dbObj = (DBObject) JSON.parse("{ $match: { $and: [ {
errorCode: { $not: {$regex : \".*\" } } } ] } }");
Post by Dwight Merriman
1) try quoting the left hand side names as shown below
2) i'm not sure if that function will do regular expressions. IIRC there
is no regular expression type in the JSON RFC, that is "javascript" json.
but BSON does support regular expression type so you can do it is just a
question of how i don't know the java driver well but it might take $regex
or you could use other means in the driver to make the BSON
http://docs.mongodb.org/manual/reference/mongodb-extended-json/
Post by Ignatij Suvorov
Hello.
JSON.parse is throwing exception
{$not: /.*/}}]}}");
How to escape / characters?
Thank you!
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Ignatij Suvorov
2013-01-24 10:28:31 UTC
Permalink
awesome, it worked, thx!

funny part is that this approach doesn't work in mongo console and it says
"errmsg" : "exception: can't use $not with $regex, use BSON regex type
instead"
I stumbled upon this error before writing this question, but it seems that
JSON.parse with JAVA Driver conjunction works a little bit different than
in mongo console.

Thanks again!
Post by Jeff Yemin
JSON.parse will handle unquoted key names, but not Javascript regular
DBObject dbObj = (DBObject) JSON.parse("{ $match: { $and: [ {
errorCode: { $not: {$regex : \".*\" } } } ] } }");
Post by Dwight Merriman
1) try quoting the left hand side names as shown below
2) i'm not sure if that function will do regular expressions. IIRC there
is no regular expression type in the JSON RFC, that is "javascript" json.
but BSON does support regular expression type so you can do it is just a
question of how i don't know the java driver well but it might take $regex
or you could use other means in the driver to make the BSON
http://docs.mongodb.org/manual/reference/mongodb-extended-json/
Post by Ignatij Suvorov
Hello.
JSON.parse is throwing exception
{$not: /.*/}}]}}");
How to escape / characters?
Thank you!
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Loading...