如何删除firestore自动生成的单字段索引?

时间:2021-12-14 04:14:33

update:
TLDR;

if you reached here, you should recheck the way you build your DB. Your document(s) probably gets expended over time (due to nested list or etc.).

更新:TLDR;如果你到达这里,你应该重新检查你构建数据库的方式。您的文档可能会随着时间的推移而消耗(由于嵌套列表等)。

Original question:
I have a collection of documents that have a lot of fields. I do not query documents even no simple queries- I am using only-

原始问题:我有一系列有很多领域的文档。即使没有简单的查询,我也不查询文件 - 我只使用 -

db.collection("mycollection").doc(docName).get().then(....);

in order to read the docs, so I don't need any indexing for this collection.

为了阅读文档,所以我不需要为此集合编制任何索引。

The issue is that firestore generates Single-field indexes automatically, and due to the amount of fields cause limitation exceeding of indexing: 如何删除firestore自动生成的单字段索引? And if I trying to add a field to one of the documents it throws me an error:

问题是firestore会自动生成单字段索引,并且由于字段数量导致限制超出索引:如果我尝试向其中一个文档添加字段,则会抛出错误:

Uncaught (in promise) Error: Too many indexed properties for entity: app: "s~myapp",path <  Element {    type: "tags",    name: "aaaa"  }>
    at new FirestoreError (index.cjs.js:346)
    at index.cjs.js:6058
    at W.<anonymous> (index.cjs.js:6003)
    at Ab (index.js:23)
    at W.g.dispatchEvent (index.js:21)
    at Re.Ca (index.js:98)
    at ye.g.Oa (index.js:86)
    at dd (index.js:42)
    at ed (index.js:39)
    at ad (index.js:37)

I couldn't find any way to delete these single-field-indexing or to tell firestore to stop generating them. I found this in firestore console: 如何删除firestore自动生成的单字段索引?

我找不到任何方法来删除这些单字段索引或告诉firestore停止生成它们。我在firestore控制台中找到了这个:

but there is no way to disable this, and to disable auto indexing for a specific collection. Any way to do it?

但是没有办法禁用它,并禁用特定集合的自动索引。有办法吗?

3 个解决方案

#1


2  

The short answer is you can't do that right now with Firebase. However, this is a good signal that you need to restructure your database models to avoid hitting limits such as the 1MB per document.

简短的回答是你现在不能用Firebase做到这一点。但是,这是一个很好的信号,您需要重构数据库模型以避免达到限制,例如每个文档1MB。

The documentation talks about the limitations on your data:

文档讨论了数据的限制:

You can't run queries on nested lists. Additionally, this isn't as scalable as other options, especially if your data expands over time. With larger or growing lists, the document also grows, which can lead to slower document retrieval times.

您无法在嵌套列表上运行查询。此外,这不像其他选项那样可扩展,特别是如果您的数据随着时间的推移而扩展。随着列表的增大或增长,文档也会增长,这会导致文档检索时间变慢。

See this page for more information about the advantages and disadvantages on the different strategies for structuring your data: https://firebase.google.com/docs/firestore/manage-data/structure-data

有关构建数据的不同策略的优缺点的详细信息,请参阅此页:https://firebase.google.com/docs/firestore/manage-data/structure-data

#2


0  

As stated in the Firestore documentation:

如Firestore文档中所述:

Cloud Firestore requires an index for every query, to ensure the best performance. All document fields are automatically indexed, so queries that only use equality clauses don't need additional indexes. If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Cloud Firestore需要为每个查询提供索引,以确保最佳性能。所有文档字段都自动编入索引,因此仅使用相等子句的查询不需要其他索引。如果尝试使用未映射到现有索引的range子句的复合查询,则会收到错误。该错误消息包含一个直接链接,用于在Firebase控制台中创建缺失的索引。

Can you update your question with the structure data you are trying to save?

你能用你想要保存的结构数据更新你的问题吗?

A workaround for your problem would be to create compound indexes, or as a last resource, Firestore may not be suited to the needs for your app and Firebase Realtime Database can be a better solution.

您的问题的解决方法是创建复合索引,或者作为最后一个资源,Firestore可能不适合您的应用程序需求,Firebase实时数据库可以是更好的解决方案。

See tradeoffs: RTDB vs Firestore

请参阅权衡:RTDB与Firestore

#3


0  

I don't believe that there currently exists the switch that you are looking for, so I think that leaves the following,

我不相信目前存在你正在寻找的开关,所以我认为留下以下内容,

  1. Globally disable built-in indexes and create all indexes explicitly. Painful and they have limits too.

    全局禁用内置索引并显式创建所有索引。痛苦,他们也有限制。

  2. A workaround where you treat your Cloud Firestore unfriendly content like a BLOB, like so:

    解决方法是将您的Cloud Firestore视为不友好的内容,如BLOB,如下所示:

To store,

储藏,

const objIn = {text: 'my object with a zillion fields' };
const jsonString  = JSON.stringify(this.objIn);
const container = { content: this.jsonString };

To retrieve,

要检索,

const objOut = JSON.parse(container.content);

#1


2  

The short answer is you can't do that right now with Firebase. However, this is a good signal that you need to restructure your database models to avoid hitting limits such as the 1MB per document.

简短的回答是你现在不能用Firebase做到这一点。但是,这是一个很好的信号,您需要重构数据库模型以避免达到限制,例如每个文档1MB。

The documentation talks about the limitations on your data:

文档讨论了数据的限制:

You can't run queries on nested lists. Additionally, this isn't as scalable as other options, especially if your data expands over time. With larger or growing lists, the document also grows, which can lead to slower document retrieval times.

您无法在嵌套列表上运行查询。此外,这不像其他选项那样可扩展,特别是如果您的数据随着时间的推移而扩展。随着列表的增大或增长,文档也会增长,这会导致文档检索时间变慢。

See this page for more information about the advantages and disadvantages on the different strategies for structuring your data: https://firebase.google.com/docs/firestore/manage-data/structure-data

有关构建数据的不同策略的优缺点的详细信息,请参阅此页:https://firebase.google.com/docs/firestore/manage-data/structure-data

#2


0  

As stated in the Firestore documentation:

如Firestore文档中所述:

Cloud Firestore requires an index for every query, to ensure the best performance. All document fields are automatically indexed, so queries that only use equality clauses don't need additional indexes. If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Cloud Firestore需要为每个查询提供索引,以确保最佳性能。所有文档字段都自动编入索引,因此仅使用相等子句的查询不需要其他索引。如果尝试使用未映射到现有索引的range子句的复合查询,则会收到错误。该错误消息包含一个直接链接,用于在Firebase控制台中创建缺失的索引。

Can you update your question with the structure data you are trying to save?

你能用你想要保存的结构数据更新你的问题吗?

A workaround for your problem would be to create compound indexes, or as a last resource, Firestore may not be suited to the needs for your app and Firebase Realtime Database can be a better solution.

您的问题的解决方法是创建复合索引,或者作为最后一个资源,Firestore可能不适合您的应用程序需求,Firebase实时数据库可以是更好的解决方案。

See tradeoffs: RTDB vs Firestore

请参阅权衡:RTDB与Firestore

#3


0  

I don't believe that there currently exists the switch that you are looking for, so I think that leaves the following,

我不相信目前存在你正在寻找的开关,所以我认为留下以下内容,

  1. Globally disable built-in indexes and create all indexes explicitly. Painful and they have limits too.

    全局禁用内置索引并显式创建所有索引。痛苦,他们也有限制。

  2. A workaround where you treat your Cloud Firestore unfriendly content like a BLOB, like so:

    解决方法是将您的Cloud Firestore视为不友好的内容,如BLOB,如下所示:

To store,

储藏,

const objIn = {text: 'my object with a zillion fields' };
const jsonString  = JSON.stringify(this.objIn);
const container = { content: this.jsonString };

To retrieve,

要检索,

const objOut = JSON.parse(container.content);