MongoDB Regex Query: Why doesn't it work?

Refer to the second last post.

Note. I used 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"   }
    ]
>
+3
source share
2 answers

I think it's just a mistake try.mongodb.org. They work for me in the local local shell mongo:

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

And the documentation says this :

You can use regular expressions 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'] } } );

Thus, both string and regression versions of RegExp are supported.

+9
source

It seems that http://try.mongodb.org/ for some reason does not support regular expressions. The real console is fine.

+2

All Articles