【发布时间】:2013-07-04 07:39:38
【问题描述】:
我想按名称查找帐户(在 50K 帐户的 MongoDB 集合中)
以通常的方式:我们用字符串查找
db.accounts.find({ name: 'Jon Skeet' }) // indexes help improve performance!
用正则表达式怎么样?这是一项昂贵的手术吗?
db.accounts.find( { name: /Jon Skeet/ }) // worry! how indexes work with regex?
编辑:
根据 WiredPrairie:
MongoDB 使用 RegEx 的 prefix 来查找索引(例如:/^prefix.*/):
db.accounts.find( { name: /^Jon Skeet/ }) // indexes will help!'
【问题讨论】:
-
@dirkk,我想获得更多的经验和解释。我也想分享这个问题。
-
要使正则表达式使用索引,它必须使用文档中所示的锚点:docs.mongodb.org/manual/reference/operator/regex
-
StackOverflow 上已经回答了许多其他非常相似的问题。
-
@WiredPrairie 我想关注性能而不是如何进行查询。
标签: regex mongodb indexing mongodb-query