MongoDB正则表达式查询:为什么不起作用? [英] MongoDB Regex Query : Why doesn't this work?

查看:89
本文介绍了MongoDB正则表达式查询:为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看倒数第二个输入.

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/})

文档中对此说:

您可以在数据库查询表达式中使用正则表达式:

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'] } } );

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正则表达式查询:为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆