开发者

MongoDB Regex Query : Why doesn't this work?

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" : "4df3b39cc开发者_运维百科c93747e68039f08"   },   "fullname" : "Derek Litz"   }
    ]
>


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜