简体   繁体   English

MongoDB C#OfType()索引行为

[英]MongoDB C# OfType() Indexing behavior

Suppose I have this query over a Collection: 假设我对一个集合有此查询:

var dbMarkers = Features.AsQueryable<DBFeature>()
                        .OfType<DBPointFeature>()
                        .Where(f => f.parentFeatureSetId = parentFeatureSetId );

And the features are indexed (sparse) by: 并且通过以下方式对特征进行索引(稀疏):

{ "parentFeatureSetId": 1 }

As I understand, the C# driver uses the Type information DBFeature and DBPointFeature as an additional filter when building the query. 据我了解,在构建查询时,C#驱动程序将类型信息DBFeatureDBPointFeature用作附加过滤器。

I would like to know if the Index is applied BEFORE the type Query, so it will be limited to the Index subset. 我想知道是否在类型Query之前应用索引,因此它将仅限于索引子集。

If not, it would be first fetching all the items of a particular type (which is a set much bigger than the idexed set), and the index would be almost pointless. 如果不是这样,它将首先获取特定类型的所有项目(该集合的大小比对象集大得多),索引将几乎毫无意义。

What is the behavior of MongoDB in this case? 在这种情况下,MongoDB的行为是什么?

Thank you very much!! 非常感谢你!!

If only parentFeatureSetId is indexed, then that will be the index MongoDB uses. 如果仅对parentFeatureSetId进行索引,则它将成为MongoDB使用的索引。 It will first find the documents with the specified parentFeatureSetId and then scan them for the ones with the right type. 它将首先查找具有指定的parentFeatureSetId的文档,然后扫描它们以查找具有正确类型的文档。

You could however have the type discriminator field ( _t ) indexed as well, which may improve performance. 但是,您也可以为类型鉴别_t字段( _t )建立索引,这可以提高性能。 In this case mongo will intersect both indexes and scan only the subset of items that have both the right type and parentFeatureSetId 在这种情况下蒙戈将交叉索引和仅扫描具有正确的类型和这两个项目的子集parentFeatureSetId


Important note: The C# driver will not use DBFeature while building the query, it will only use DBPointFeature which you noted the documents must be specifically. 重要说明: C#驱动程序在构建查询时将不会使用DBFeature而只会使用您已注意到文档必须特别注明的DBPointFeature The generic AsQueryable is only there to ease the usage. 通用的AsQueryable仅用于简化用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM