regex over http interface

81 views
Skip to first unread message

Ben-Alexander Cassell

unread,
Sep 20, 2012, 1:33:41 PM9/20/12
to mongod...@googlegroups.com
Do any of the available http interfaces to mongodb support the full query specification, in particular regular expressions?  I've been playing around with using sleepy.mongoose because it generates a reasonable response much quicker than the library I'm using (mongoid) when the responses are very large.  Unfortunately, it doesn't look as though sleepy.mongoose supports regex queries which are absolutely essential to my project.  Do any of the alternative http interfaces provide this ability?

Sam Helman

unread,
Sep 21, 2012, 4:30:12 PM9/21/12
to mongod...@googlegroups.com
It might be a good idea to try DrowsyDromedary at https://github.com/zuk/DrowsyDromedary.  It looks like the query json is pulled directly from the url and used as a query, so if you can url encode your regexp it should be possible to send one over.

A. Jesse Jiryu Davis

unread,
Sep 21, 2012, 5:51:32 PM9/21/12
to mongod...@googlegroups.com
Sleepy Mongoose supports regex queries. Check out the tutorial:


If I insert a document in the MongoDB shell:

> db.test.insert({"a": "foo"})

... then I can find it from the shell like:

> db.test.find({"a": /fo+/})
{ "_id" : ObjectId("505cda7b71f3759c62076b13"), "a" : "foo" }

or:
> db.test.find({"a": {"$regex": "fo+", "$options": ""}})
{ "_id" : ObjectId("505cda7b71f3759c62076b13"), "a" : "foo" }

So to format a query for Sleepy Mongoose, first URL-escape the query using Javascript's built-in escape() function. Note the single-quotes around the query:

escape('{"a":{"$regex":"fo.*","$options":""}}')
%7B%22a%22%3A%7B%22%24regex%22%3A%22fo.*%22%2C%22%24options%22%3A%22%22%7D%7D

Then plug that into Sleepy Mongoose:


{"ok": 1, "results": [{"a": "foo", "_id": {"$oid": "505cda7b71f3759c62076b13"}}], "id": 8}

Ben-Alexander Cassell

unread,
Sep 21, 2012, 6:07:07 PM9/21/12
to mongod...@googlegroups.com
The missing piece of the puzzle was the "options" entry.  Without it, the regex query does not work in sleepy.mongoose, but does in mongodb.  Thanks!
Message has been deleted
Message has been deleted

Ben-Alexander Cassell

unread,
Sep 23, 2012, 3:42:20 PM9/23/12
to mongod...@googlegroups.com
Don't know why, but it appears some pertinent later messages have been deleted from the thread.  encodeURI fails to properly encode a + in a regular expression, and no manner of manipulation seems to allow sleepy mongoose to understand \d.

A. Jesse Jiryu Davis

unread,
Sep 23, 2012, 5:43:15 PM9/23/12
to mongod...@googlegroups.com
Yeah, I saw that too. Let me know if you find a fix; Sleepy Mongoose may need a bugfix for unquoting regular expressions from the URL.
Reply all
Reply to author
Forward
0 new messages