Indeed, this works fine in the shell. The PHP issue was a double-escaping
issue (my PHP application defines the Mongo functions) an it has been
resolved. Thanks for your help! Thanks to MongoDB I've increased the
performance of Tera-WURFL by almost 2x and I'll be pushing it hard in the
upcoming release.
Thanks,
Steve Kamerman
Sent from my iPhone
On Sep 3, 2010, at 7:52 AM, Kristina Chodorow <kris### @10gen.com>
wrote:
I think this is a shell bug. You can get around it by doing:
user_agent : new RegExp("^Mozilla/")
Similarly, you shouldn't have to escape the / in PHP.
On Fri, Sep 3, 2010 at 1:26 AM, Tera-WURFL
<stevek### @gmail.com> wrote:
I'm trying find all documents with a string that starts with a given
string. For example, I would look for "app" and expect to find
"apple" but not "aptitude". In my case, I'm actually searching for
browser User Agent prefixes, which occasionally end in a "/"
character. It appears that it is impossible to do a RegEx search for
a string ending in "/" in MongoDBs PCRE search in an efficient way.
I'm using the PHP MongoDB client, but I can reproduce the problem from
the shell:
I'm trying to find all documents with a "user_agent" that starts with
"Mozilla/" by using the regex /^Mozilla\// (I'm escaping the "/" with
a "\")
Try #1: failed to parse - Mongo is looking for the end of the RegEx I
think...
> db.TeraWurfl_MSIE.find({user_agent:/^Mozilla\//},{user_agent:1})
... ^C
Try #2: If I add a space after the "/" it parses, but doesn't find any
documents since there isn't supposed to be a space there
> db.TeraWurfl_MSIE.find({user_agent:/^Mozilla\/ /},{user_agent:1})
Try #3: This is less efficient since it scans for an extra optional
character, but it does work and it's better than using ".*" since that
would result in a complete string scan.
>
db.TeraWurfl_MSIE.find({user_agent:/^Mozilla\/.?/},{user_agent:1})
{ "_id" : ObjectId("4c806876077db9a3161a6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; mowser;
http://www.mowser.com)" }
{ "_id" : ObjectId("4c806876077db9a3161b6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; XV6875.1)" }
{ "_id" : ObjectId("4c806876077db9a3161c6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE" }
{ "_id" : ObjectId("4c806876077db9a3161d6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 4.0;" }
{ "_id" : ObjectId("4c806876077db9a3161e6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 5.0;" }
{ "_id" : ObjectId("4c806876077db9a3161f6900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 5.5;" }
{ "_id" : ObjectId("4c806876077db9a316206900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 6.0;" }
{ "_id" : ObjectId("4c806876077db9a316216900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 7.0;" }
{ "_id" : ObjectId("4c806876077db9a316226900"), "user_agent" :
"Mozilla/4.0 (compatible; MSIE 8.0;" }
Is there a better way for me to escape strings before inserting them
in regexps?
Also, I am using MongoDB 1.6.2 on Ubuntu 10.04
Thanks!
Steve Kamerman
http://www.tera-wurfl.com
--
(61 lines) Sep 3, 2010 04:16
(70 lines) Sep 3, 2010 07:53