在多个阵列字段上提高MongoDB性能 [英] Increasing MongoDB performance on multiple array fields

查看:438
本文介绍了在多个阵列字段上提高MongoDB性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的MongoDb集合,用户可以在其中搜索多个Array字段。索引的最佳方法是什么?我看到为多个Array索引创建索引最终得到:

I have a large MongoDb collection, where users can search on multiple Array fields. How is the best way to index that? I saw that creating indexes for multiple Array indexes ends up with:

Mongo :: Error :: OperationFailure:无法索引并行数组

示例查询:

db.collection.find(
   {category_ids: {$in: [object_id, object_id]}, 
    second_category_ids: $in: {[object_id, object_id]}
   }
)


推荐答案

根据mongoDB 文档,不允许在并行数组上使用多键索引。下面是文档的内容 -

As per mongoDB documentation, multikey index on parallel arrays is not allowed. Below is what documentation reads-


因此,如果有多个要索引的字段,则无法创建复合多键索引document是一个数组。或者,如果已存在复合多键索引,则无法插入会违反此限制的文档。

As such, you cannot create a compound multikey index if more than one to-be-indexed field of a document is an array. Or, if a compound multikey index already exists, you cannot insert a document that would violate this restriction.

我们可以做的最多是分别索引两个字段,查询将使用它感觉到的最佳索引。如果你有比查询计划器更好的想法,那么也可以使用提示来使用特定的索引。

At max we can do is index both fields separately and query will use the best index it feels. If you have better idea than query planner then hint can also be used to use specific index.

或者你可以考虑重构模式。例如,子类别可以采用不同的集合。如果我了解有关数据模式和架构设计的更多信息,我可以提供帮助。

Or you can think for restructuring your schema. For example subcategories can be taken in different collection. May be I can help if I get to know more about data pattern and schema design.

这篇关于在多个阵列字段上提高MongoDB性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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