在Mongodb中索引并行数组 [英] Indexing parallel arrays in Mongodb

查看:86
本文介绍了在Mongodb中索引并行数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用MongoDb C#,但是遇到了一个小问题.

I am starting to use MongoDb C#, but have run into a slight issue.

因此,我有一个包含2个嵌入式集合(不同类型)的文档.我想搜索这两个集合的字段,但是我发现,如果尝试对2个集合的可搜索字段建立索引,则会得到无法索引并行数组"的信息.在阅读有关多键索引的Mongodb文档时,我发现这确实是一个限制.

So I have a document with 2 embedded collections(of distinct types). I want to search on fields of both of these collections however I have discovered that if I try to index the searchable fields on the 2 collections I get "cannot index parallel arrays". Reading the Mongodb documentations on multikey indexes I discovered that this is indeed a limitation.

我的问题是,关于此问题的正常解决方法是什么?我不能真正地合并这些集合,因为它们非常不同吗?我应该遵循什么模式?

My question is what is the normal work around regarding this issue? I cant really combine these collections since they are pretty distinct? What pattern should I follow?

public class Capture
{
        [BsonId]
        public Guid Id { get; set; }
        ...Some other fields
        public IList<CustomerInformation> CustomerInformations { get; set; }
        public IList<VehicleLicenseDisk> VehicleLicenseDisks { get; set; }
}

推荐答案

在讨论可能的解决方法之前,我只想强调一下MongoDB为什么选择 来对索引并行数组实施此限制.当您在MongoDB中为数组建立索引时,它将创建一个多键索引,每个数组元素具有一个键.因此,如果在两个数组上创建一个复合索引,一个数组具有M个不同的值,而另一个数组具有N个不同的值,则该索引本质上具有MN键.这是非常糟糕的-不同数组元素的数量是非线性的.考虑添加或删除数组元素时维护这样的索引所需的工作量.

Before talking about possible workarounds, I just want to highlight why MongoDB has chosen to enforce this restriction on indexing parallel arrays. When you index an array in MongoDB, it creates a multikey index with one key per array element. Therefore, if you create a compound index on two arrays, one with M distinct values and one with N distinct values, the index essentially has MN keys. This is very bad- it's nonlinear in the number of distinct array elements. Consider the amount of work it takes to maintain an index like this when you add or remove array elements.

好的,除了理由,要解决此限制,使用当前支持索引交集的MongoDB版本(2.6)将很有帮助.可以在CustomerInformationsVehicleLicenseDisks上创建索引,然后MongoDB可以使用这两个索引并将它们相交以提供对这两个都有限制的查询.

OK, justification aside, to work around this restriction it will be helpful to use the current MongoDB version (2.6), which supports index intersection. One can create an index on CustomerInformations and VehicleLicenseDisks and then MongoDB can use both indices and intersect them to serve queries that have restrictions on both.

无论出于何种原因,如果您坚持使用MongoDB< 2.6,那么您的选择要么是考虑重新设计架构,要么是取决于最多使用数组字段之一的索引.

If you are, for whatever reason, stuck with MongoDB < 2.6, then your options are either to consider redesigning the schema or to depend on indexes that use at most one of the array fields.

这篇关于在Mongodb中索引并行数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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