skip to Main Content

So let’s say you have 3 or more courses like c#, c++, java, and Python and you would like to implement some chatting feature on each course. Would you create like different collections to store their messages separately in cloud firestore then use where clause on let’s say field ==’python’?
If subcollection could it be created automatically? (on first message sent maybe)

2

Answers


  1. For example you have 10 courses with 100 messages each the data it would send to each call is 1000 entries and if you have a separate collection then you get only 100 entries. It is always better to reduce the amount of data passed

    Login or Signup to reply.
  2. Would you create different collections to store their messages separately in Cloud Firestore?

    Yes, you can do that. A possible Firestore schema might look like this:

    Firestore-root
       |
       --- courses (collection)
             |
             --- $courseId (document)
             |      |
             |      --- name: "Java"
             |      |
             |      --- chats (sub-collection)
             |           |
             |           --- $chatId (document)
             |                 |
             |                 --- message: "Hello Java!"
             |
             --- $courseId (document)
                    |
                    --- name: "C#"
                    |
                    --- chats (sub-collection)
                         |
                         --- $chatId (document)
                               |
                               --- message: "Hello C#!"
    

    In this way, you can simply get the messages that correspond to a single course, by pointing exactly to the "chats" sub-collection, that exists, within that course. If you however want to get all messages, from all courses, a collection group query will be required, which in code it looks like this:

    Firestore.instance.collectionGroup('chats').getDocuments()
    

    use where clause on let’s say field ==’python’?

    You can also create a top-level collection that can look like this:

    Firestore-root
       |
       --- chats (collection)
            |
            --- $chatId (document)
                  |
                  --- message: "Hello Python!"
                  |
                  --- course: "python"
    

    From each, you can get all messages of a particular course using field =='python'.

    If subcollection could it be created automatically? (on the first message sent maybe)

    Yes, this is possible. Simply create the corresponding reference and add the first document (chat) to the "chats" collection or sub-collection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search