3

See second to last input please.

Note: I was using http://try.mongodb.org/

> person = {fullname : "Derek Litz"}
{
     "fullname" : "Derek Litz"
     }
> db.people.save(person)
"ok"
> db.people.find()

    [ 
      {   "_id" : {   "$oid" : "4df3b39ccc93747e68039f08"   },   "fullname" : "Derek Litz"   }
    ]
> db.people.find({fullname : 'Derek Litz'})

    [ 
      {   "_id" : {   "$oid" : "4df3b39ccc93747e68039f08"   },   "fullname" : "Derek Litz"   }
    ]
> db.people.find({fullname : /^D.*/})

    [ 
      
    ]
> db.people.find({fullname : {$regex : '^D.*'}})

    [ 
      {   "_id" : {   "$oid" : "4df3b39ccc93747e68039f08"   },   "fullname" : "Derek Litz"   }
    ]
>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
Derek Litz
  • 10,529
  • 7
  • 43
  • 53

2 Answers2

9

I think that's just a bug in try.mongodb.org. These work for me in my local mongo shell:

db.people.find({first_name: {$regex: /e/}})
db.people.find({first_name: /e/})

And the documentation says this:

You may use regexes in database query expressions:

db.customers.find( { name : /acme.*corp/i } );
db.customers.find( { name : { $regex : 'acme.*corp', $options: 'i' } } );
[...]
db.customers.find( { name : { $regex : /acme.*corp/i, $nin : ['acmeblahcorp'] } } );

So both string and RegExp literal versions are supported.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
2

It seems, that http://try.mongodb.org/ just doesn't support regular expressions for some reason. Real console is ok.

Jake Jones
  • 1,170
  • 1
  • 11
  • 15
  • 2
    try.mongo.org supports regular expressions in string literals but doesn't seem to understand RegExp literals. So it does support them but its syntax doesn't completely line up with the documentation, the real mongo shell, or the real full blown mongo query interface. – mu is too short Jun 11 '11 at 19:05
  • @muistooshort - so what syntax should you use in the try.mongo? – UpTheCreek Mar 27 '13 at 12:00
  • @UpTheCreek: I'm not certain but maybe `new RegExp(...)` would work. – mu is too short Mar 27 '13 at 15:45