问题描述
请查看倒数第二个输入.
See second to last input please.
注意:我正在使用 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" }
]
>
推荐答案
我认为这只是try.mongodb.org
中的错误.这些在我本地的mongo
shell中为我工作:
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/})
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'] } } );
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'] } } );
因此,字符串和RegExp文字版本均受支持.
So both string and RegExp literal versions are supported.
这篇关于MongoDB正则表达式查询:为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!