简体   繁体   English

文档中的Mongodb Indexing array字段

[英]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.

相关问题 MongoDB中许多具有相同字段或一个数组的文档 - Many documents with the same field or one array in MongoDB MongoDB:如何按数组字段对文档进行排序? - MongoDB : How to sort documents by array field? mongodb - 将多个文档的数组字段连接到一个数组字段 - mongodb - concat array field from many documents to one array field 如何在MongoDb中查找与数组中的字段和子文档字段匹配的文档 - How to find documents in MongoDb matching a field and subdocument field that are in an array 在mongoDB中索引大型数组 - indexing large array in mongoDB 在MongoDB中查找其数组字段包含某些子集的文档 - Find documents whose array field contain some subsets in MongoDB 如何将文档流聚合成一个带有数组字段的文档? (mongodb) - How to aggregate documents flow into a single document with an array field? (mongodb) 按数组字段的元素索引顺序查找 mongodb 集合中的文档 - Finding documents in mongodb collection by order of elements index of array field MongoDB 查询文档数组 - MongoDB querying on array of documents 如何在Mongodb(Mongoid)中使用包含另一个文档的数组字段创建模型作为嵌入式文档 - How to create model with an array field which contains another documents as an embedded documents in Mongodb (Mongoid)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM