Maximum number of fields for a Firestore document?

◇◆丶佛笑我妖孽 提交于 2020-05-16 01:41:30

问题


Right now I have a products collection where I store my products as documents like the following:

documentID:

title: STRING,
price: NUMBER,
images: ARRAY OF OBJECTS,
userImages: ARRAY OF OBJECTS,
thumbnail: STRING,
category: STRING

NOTE: My web app has approximately 1000 products.

I'm thinking about doing full text search on client side, while also saving on database reads, so I'm thinking about duplicating my data on Firestore and save a partial copy of all of my products into a single document to send that to the client so I can implement client full text search with that.

I would create the allProducts collection, with a single document with 1000 fields. Is this possible?

allProducts: collection

Contains a single document with the following fields:

Every field would contain a MAP (object) with product details.

document_1_ID: {  // Same ID as the 'products' collection
  title: STRING,
  price: NUMBER,
  category: STRING,
  thumbnail
},
document_2_ID: {
  title: STRING,
  price: NUMBER,
  category: STRING,
  thumbnail
},
// AND SO ON...

NOTE: I would still keep the products collection intact.

QUESTION

Is it possible to have a single document with 1000 fields? What is the limit?

I'm looking into this, because since I'm performing client full text search, every user will need to have access to my whole database of products. And I don't want every user to read every single document that I have, because I imagine that the costs of that would not scale very well.

NOTE2: I know that the maximum size for a document is 1mb.


回答1:


According to the documentation, there is no stated limit placed on the number of fields in a document. However, a document can only have up to 40,000 index entries, which will grow as documents contain more fields that are indexed by default.




回答2:


I would create the allProducts collection, with a single document with 1000 fields. Is this possible?

You can add as much data as you want in a document, but only as long as you stay within the limit. So the problem isn't the fact the that you duplicate data, the problem is that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but as your documents getts bigger, be careful about this limitation.

If you are storing large amount of data in your documents and those documents should be updated by lots of admins, there is another limitation that you need to take care of. So you are limited to 1 write per second on every document. So if you have a situation in which the admins are trying to write/update products in that same document all at once, you might start to see some of this writes to fail. So, be careful about this limitation too.

Is it possible to have a single document with 1000 fields?

It is possible up to 40,000 properties but in your case with no benefits. I say that because everytime you perform a query (get the document), only a single document will be returned. So there is no way you can implement a search algorithm in a single documents and expect to get Product objects in return.

And I don't want every user to read every single document that I have, because I imagine that the costs of that would not scale very well.

Downloading an entire collection to search for fields client-side isn't practical at all and is also very costly. That's the reason why the official documentation recommends a third-party search service like Algolia.

For Android, please see my answer from the following post:

  • Is it possible to use Algolia query in FirestoreRecyclerOptions?



回答3:


According to this document, in addition to the 1MB limit per document, there is a limit of index entries per document, which is 40,000. Because each field appears in 2 indexes (ascending and descending), the maximum number of fields is 20,000.

I made a Node.js program to test it and I can confirm that I can create 20,000 fields but I cannot create 20,001.

If you try to set more than 20,000 fields, you will get the exception:

INVALID_ARGUMENT: too many index entries for entity

  // Setting 20001 here throws "INVALID_ARGUMENT: too many index entries for entity"
  const indexPage = Array.from(Array(20000).keys()).reduce((acc, cur) => {
    acc[`index-${cur}`] = cur;
    return acc;
  }, {});
  await db.doc(`test/doc`).set(indexPage);


来源:https://stackoverflow.com/questions/56428608/maximum-number-of-fields-for-a-firestore-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!