firestore: arrays vs sub collection of documents performance

Jimmy H.

i would like to ask if there is a best practice for firestore, when one develops a chat app, and what is the best practice to store messages for chat-rooms.

The assumption here is that every chatroom has its own document.

I started using an array to store the messages from the users. The problem with that approach is that there is no way to add, a insert(append) a new entry everytime a new message is submitted to the chat room. One has to save a new copy of the array with the new message appended. This seems like something that would scale really bad, unless the chat history is split in sub-arrays etc..

In the official documents, they suggest a structure, where one should store the messages of a specific chatroom as separate documents in a sub collection of that chatroom. I wonder if this approach is the best, and what would be some drawbacks, or if there is another preferred way to do this.

Todd Kerpelman

I would generally go with the approach of "Every chat room has a subcollection of messages. And every new message is a separate document in this subcollection." This has several advantages: It's easy to add or edit individual messages, and you can perform a number of different queries (like "Grab the 20 most recent messages")

The biggest drawback, I suppose, is that if you find that new users are frequently going to be entering your chat and will want to see the entire chat history of the room up until they joined, that would result in a large number of database reads. Realistically, though, I don't know how often that would happen in real life, and you could mitigate this by using pagination to grab your historical chat in batches.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Working with Firestore documents and collection data

分類Dev

ternary operator vs [collection if].first performance?

分類Dev

How to get sub-collections of every collection in Firestore using Flutter

分類Dev

OpenCL performance: using arrays of primitives vs arrays of structures

分類Dev

How to get all documents from a firestore collection and return them in an array list?

分類Dev

Are empty documents in Firestore deleted?

分類Dev

nginx-gridfs: control which documents in a collection can be served via nginx vs my app?

分類Dev

Set a collection as a sub-collection

分類Dev

Get All documents in a Collection CosmosDb

分類Dev

Firestore collection link

分類Dev

How to get matched sub documents from mongodb?

分類Dev

Performance of choice vs randint

分類Dev

Slow Swift Arrays and Strings performance

分類Dev

Call to firestore collection() not working on expo

分類Dev

Multiple triggers on the same firestore collection

分類Dev

Search for a field in a collection on firebase firestore

分類Dev

Firestore collection querying on map field

分類Dev

How to show collection list in firestore?

分類Dev

Removing all documents and collections from the Firestore

分類Dev

Flutter firestore fetch documents with a condition on a map field

分類Dev

Find all documents in a collection with mongo go driver

分類Dev

How to stop insertion of Duplicate documents in a mongodb collection

分類Dev

Get the count of the number of documents in a Collection Mongodb

分類Dev

Get All 'documents' from MongoDB 'collection'

分類Dev

How to get all documents from a collection in FaunaDB?

分類Dev

Fast query and deletion of documents of a large collection in MongoDB

分類Dev

FluentValidation checking for duplicate entity in a sub-collection

分類Dev

Flutter Web Firebase - Get collection by sub-collection field

分類Dev

http HEAD vs GET performance

Related 関連記事

  1. 1

    Working with Firestore documents and collection data

  2. 2

    ternary operator vs [collection if].first performance?

  3. 3

    How to get sub-collections of every collection in Firestore using Flutter

  4. 4

    OpenCL performance: using arrays of primitives vs arrays of structures

  5. 5

    How to get all documents from a firestore collection and return them in an array list?

  6. 6

    Are empty documents in Firestore deleted?

  7. 7

    nginx-gridfs: control which documents in a collection can be served via nginx vs my app?

  8. 8

    Set a collection as a sub-collection

  9. 9

    Get All documents in a Collection CosmosDb

  10. 10

    Firestore collection link

  11. 11

    How to get matched sub documents from mongodb?

  12. 12

    Performance of choice vs randint

  13. 13

    Slow Swift Arrays and Strings performance

  14. 14

    Call to firestore collection() not working on expo

  15. 15

    Multiple triggers on the same firestore collection

  16. 16

    Search for a field in a collection on firebase firestore

  17. 17

    Firestore collection querying on map field

  18. 18

    How to show collection list in firestore?

  19. 19

    Removing all documents and collections from the Firestore

  20. 20

    Flutter firestore fetch documents with a condition on a map field

  21. 21

    Find all documents in a collection with mongo go driver

  22. 22

    How to stop insertion of Duplicate documents in a mongodb collection

  23. 23

    Get the count of the number of documents in a Collection Mongodb

  24. 24

    Get All 'documents' from MongoDB 'collection'

  25. 25

    How to get all documents from a collection in FaunaDB?

  26. 26

    Fast query and deletion of documents of a large collection in MongoDB

  27. 27

    FluentValidation checking for duplicate entity in a sub-collection

  28. 28

    Flutter Web Firebase - Get collection by sub-collection field

  29. 29

    http HEAD vs GET performance

ホットタグ

アーカイブ