Adding a 'tree' to my Firebase document

205 views
Skip to first unread message

Sharon Gilmore

unread,
Jul 21, 2022, 10:53:14 AM7/21/22
to Firebase Google Group
Hi everyone, I'm looking for some advice on structuring my Firestore database.  Use case is that I have a series of pages, which I want to be able to list by tag (a page has only one tag).  A page can have child pages, so for a specific tag I could have the following:

Page 1
    Page 1a
    Page 1b
Page 2
    Page 2a
       Page 2a.1
       Page 2a.2
    Page 2b
Page 3
Page 4 etc.

The 'pages' collection is straightforward; each document has the following: id, title, content, parentId, dateCreated and tag (all are strings apart from dateCreated which is a timestamp).

I also have a 'tags' collection; each document has id & name (both strings).

My question is how best to store the structure above.  At various points I will need to view the pages for a particular tag; I will only need to show the title and date created for each one (and will need to know the id), but will also have to show the structure.

I'm thinking of adding a 'tree' field to the topic document, which would be something like this for the structure above:

tree: [
    0: {
        id: page1ID,
        name: 'Page 1',
        dateCreated: 21st July 2022,
        tree: [
            0: {
                id: page1aID,
                name: 'Page 1a',
                dateCreated: 21st July 2022,
                tree: []
            },
            1: {
                 id: page1bID,
                 name: 'Page 1b',
                 dateCreated: 21st July 2022,
                 tree: []
             },
        ] 
    },
    1: {
         id: page2ID,
         name: 'Page 2',
         dateCreated: 21st July 2022,
         tree: [
             0: {
                  id: page2aID,
                  name: 'Page 2a',
                  dateCreated: 21st July 2022,
                  tree: [
                       0: {
                           id: page2a1ID,
                           name: 'Page 2a1',
                           dateCreated: 21st July 2022,
                           tree: []
                       },
                       1: {
                           id: page2b1ID,
                           name: 'Page 2b1',
                           dateCreated: 21st July 2022,
                           tree: []
                       }
                  ]
            },
            1: {
                id: page2bID,
                name: 'Page 2b',
                dateCreated: 21st July 2022,
                tree: []
            },
        ]
   }
... etc ...
]

However, this is difficult (not impossible) to add pages to, and to reorder etc.  Also I'm not sure that Firestore is a big fan of saving arrays - it seems to convert them to objects first - which suggests that using arrays is not a recommended approach.

Is there a standard way of handling this kind of data? Thanks!

Kato Richardson

unread,
Jul 21, 2022, 12:09:58 PM7/21/22
to Firebase Google Group
Hello Sharon,

Great to meet you. Welcome!

Have you checked out our Get to Know Firestore series? Episodes 45 deal entirely with data structures and architecture strategies.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/08409dee-7a45-4679-ab7f-e990fd719dc7n%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Sharon Gilmore

unread,
Jul 22, 2022, 10:19:59 AM7/22/22
to Firebase Google Group
Oh, I haven't come across those - will check them out now.  Thanks!

Kato Richardson

unread,
Jul 22, 2022, 12:01:13 PM7/22/22
to Firebase Google Group
Great. If you still need some ideas, let us know.

Sharon Gilmore

unread,
Jul 26, 2022, 10:53:17 AM7/26/22
to Firebase Google Group
Thanks.  After watching the video, I realised I was overcomplicating things. So I ended up changing the tree structure to just a flat map of nodes, each of which stores its parent id and order.  Since I'm only ever showing the entire tree, I just need to retrieve all the nodes, then filter down to the ones with null parentId, and put them in order, and then filter again to get the appropriate child nodes for each one and again sort those.  I've rejigged a couple of other data structures in my project too, so it's looking a bit cleaner now!
Reply all
Reply to author
Forward
0 new messages