关于地图和数组上的Firestore索引:地图和数组上的Firestore索引-澄清

Firestore index on maps and array - clarification

我试图了解Firestore如何在字段上创建索引。 给定以下样本文档,如何创建索引,尤其是对于地图/数组?

enter image description here

我多次阅读了Cloud Firestore中"索引类型"上的文档,但仍不 那里说:

Automatic indexing
By default, Cloud Firestore automatically maintains single-field indexes for each field in a document and each subfield in a map. Cloud Firestore uses the following default settings for single-field indexes:

  • For each non-array and non-map field, Cloud Firestore defines two collection-scope single-field indexes, one in ascending mode and one in descending mode.

  • For each map field, Cloud Firestore creates one collection-scope ascending index and one descending index for each non-array and non-map subfield in the map.

  • For each array field in a document, Cloud Firestore creates and maintains a collection-scope array-contains index.

  • Single-field indexes with collection group scope are not maintained by default.

如果我正确理解这一点,那么将为每个字段创建索引,即使对于alternate_names数组中的值也是如此。

因此,如果我要搜索fields.alternate_names包含(例如)" Caofang"值的任何文档,则Firestore将使用索引进行搜索

我的假设/理解正确吗?


不,您的理解不正确。 fields.alternate_names是map字段中的一个数组子字段,这意味着它不能满足第二点的要求。 您只需发出查询即可测试您的假设。 如果查询失败,您将在错误消息中看到由于缺少索引而导致查询失败。

Firestore完全不允许未索引的查询。 失败的错误消息将包含指向控制台的链接,如果可能的话,该链接将使您可以创建该查询所需的索引。

如果您希望能够查询fields.alternate_names的内容,请考虑将其提升为自己的顶级字段,默认情况下会对其建立索引。