I'm looking for a document design to store user ids that other users liked. My first approach (maybe not the best) is an array to store all users ids that liked someone. Something like:
/users/1
- likedByUserIds: [1,2,3,4,5,...,1000,1000001,...]
I have to query by not liked users yet and show them first. So how many ids can be stored in a field array in firestore collection ? It can be many user ids from previous/past likes.
CodePudding user response:
The number of elements of the array is not fixed. The limit you will run into is the max total size of the document (1MB), which is documented here. The total size of the document is not going to be based on a single field. It will be based on everything you put in it. You can estimate the size of a document using information here.
For lists of data that are not bounded within the limits of a single document, it's better to store the items as individual docments in a collection. There is no bound to the number of documents in a collection.
CodePudding user response:
Just what @Doug stated, "there's a maximum total size for a document: 1MB". There's also a Firestore single-field index exemption limit which is 200. A single-field index stores a sorted mapping of all the documents in a collection that contain a specific field. Each entry in a single-field index records a document's value for a specific field and the location of the document in the database.
Additionally, there's a single-field index exemptions that you can exempt a field from your automatic indexing settings by creating a single-field index exemption. An indexing exemption overrides the database-wide automatic index settings. Since we need to hold that information in memory of every process that could handle a write to your database, and compare the incoming write to this list of exemptions. This creates a need for both memory management and latency impact.
For further reference, you can check this related post about Firestore single-field index exemption limit.