Home > database >  How many number of key-value pairs in a map I can store in a Firestore document?
How many number of key-value pairs in a map I can store in a Firestore document?

Time:10-17

User
--user1
  --fullName: "James Gosling"
  --messages
    --messageId1: true,
    --messageId2: true,
    --messageId3: true
--user2

I just want to store the id of each messages inside the user document so that whenever I want to update the fullName of a user, I just have to fetch those message ids and create a batched write to update the user's fullName in User Collection and Messages Collection. So how many number of key-value pairs in a Map field I can store?

CodePudding user response:

There isn't a fixed limit but Firestore recommends having less than 100 fields per document. A document also has a max size limit of 1 MB.

By default, Cloud Firestore automatically maintains single-field indexes for each field in a document and each subfield in a map.

Creating many sub-fields with different keys will also index them. You can add exemptions however your message IDs seem be totally random and I doubt if you'll know them beforehand.

If you just want to store the message IDs, then you can use an array instead:

{
  username: "test",
  messageIds: ["messageId1", "messageId2"]
}

Alternatively, you can store user's UID in the message documents and then update the documents sent by the user so you don't have to worry about the document size limit. This Gist might be helpful to write that function.

  • Related