简体   繁体   中英

How many ids can be stored in a field array in firestore collection

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.

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.

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.

Here a solution I found to avoid full search in firebase to perform get users that haven't liked yet .

  • My users collection in firestore
  • Add the user to redis add('users',userId)
  • Add liked user to redis zAdd('liked_users_${uid}',{value: userId})
  • Generate not liked yet zDiffStore('not_liked_yet',['users','liked_users_${uid}']
  • Get not liked yet zRange('not_liked_yet', 0, -1)

It just worked !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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