[英]Mongodb Indexing array field in the documents
I am newbie, trying to index a contents collections. 我是新手,正尝试为目录集合建立索引。 My query looks like
我的查询看起来像
db.contents.find({title:{$regex: /arnab/i}, tags:{$regex: /Times/i}})
below is my index query 以下是我的索引查询
db.contents.createIndex({title: 1, tags:1})
tags is any array. 标签是任何数组。 Will this query work.
此查询有效吗? Is there any better way to index title, and array tags.
有没有更好的方法来索引标题和数组标签。
Thanks in advance 提前致谢
Array fields can be indexed just like normal fields. 数组字段可以像普通字段一样建立索引。 On indexing, MongoDB creates multi-indexes for arrays fields automatically.
在建立索引时,MongoDB自动为数组字段创建多索引。
db.contents.find({title:{$regex: /arnab/i}, tags:{$regex: /Times/i}})
is a case insensitive
contains
search using regular expressions. db.contents.find({title:{$regex: /arnab/i}, tags:{$regex: /Times/i}})
是case insensitive
contains
使用正则表达式的搜索。
From the official docs: 从官方文档:
For case insensitive regular expression queries, these queries generally cannot use indexes effectively.
对于不区分大小写的正则表达式查询,这些查询通常无法有效使用索引。
So, the index you have created can not be utilised well with this query. 因此,此查询不能很好地利用您创建的索引。
Regex based searches use indexes better when the search is case sensitive. 当搜索区分大小写时,基于正则表达式的搜索可以更好地使用索引。 Eg
db.contents.find({title:{$regex: /arnab/}, tags:{$regex: /Times/}})
. 例如
db.contents.find({title:{$regex: /arnab/}, tags:{$regex: /Times/}})
。
And even better when the search is starts with (Prefix Regex)
instead of contains
. 而且更好的是,搜索
starts with (Prefix Regex)
而不是contains
。 Eg db.contents.find({title:{$regex: /^arnab/}, tags:{$regex: /^Times/}})
. 例如
db.contents.find({title:{$regex: /^arnab/}, tags:{$regex: /^Times/}})
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.