Eugene
Eugene

Reputation: 9474

Document stores (e.g. Firebase) - smaller documents or more updates?

I am learning Firebase after many years of using SQL RDBMSs. This is definitely a challenge.

Say, I have a collection of objects. Each object can belong to any number of categories. Categories have user-editable labels (e.g. user may rename the label after the fact.

SQL RDBMS

So, in RDBMS I would have:

Object table -> { object_id, ... }

Category table -> { category_id, label, ... }

ObjectCategory -> { object_id, category_id }

I see the following options to implement this in Firebase:

1. Objects collection with category label arrays in objects:

/user/objects -> [{ object_id, categories: [ 'category_label1', 'category_label2' ] }, ... ]

Seems yucky. Renaming/deleting a category will mean updating all the objects.

2. Objects referring categories by id

/user/objects -> [{ object_id, categories: [ 'category_id1', 'category_id2' ] }, ... ]

/user/categories -> [{category_id, label, is_deleted: false}, ...]

This seems more reasonable and maintainable. Except sometimes (I think pretty rarely) there will be 2 queries.

3. Collection of object and object categories

/user/objects -> [{object_id1, ...}, {object_id2, ...}]

/user/object_id1/labels -> [{categorylabel1}, {categorylabel2}]

This is largely comparable to option 1 but requires less churn on object documents and makes updates smaller. Renaming/deleting a category becomes a pain.

So, what is the recommended approach?

Upvotes: 0

Views: 22

Answers (0)

Related Questions