brooding_goat
brooding_goat

Reputation: 175

data modeling & security rules advice firebase

I am trying to model 2 concepts in firestore and also associate

collection: users
key/document_id: email
document: profile info

collection: topics
key/document_id: random
document: metadata with a field indicating email of user (to use for lookups)

My goal is to

Are both of above feasible in Firebase. Appreciate any pointers!

Upvotes: 0

Views: 66

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83048

Preamble: There isn't ONE and only ONE correct approach in NoSQL data modelling

Your approach seems valid, however I would suggest the following adaptations:

"Reference topics in users for easy lookups":

To "reference topics in users for easy lookups" you could duplicate the list of topics in an array in the user profile. You will then be able to use array-contains (and other array membership methods) for your queries. (Note however the limitation of the in operator).

Advantage of this approach: you only need to query one document to get all the topics of a user. Possible drawback: there is a limit on the size for a document (and for a single field value) which is maximum 1 MiB (1,048,576 bytes), see the doc.

You can easily keep in sync the topics array and the topics sub-collection by combining a batched write and the arrayUnion() and arrayRemove() methods.


Use the user ID instead of the email for doc Ids and Security Rules:

Instead of using the email as the users collection document ID and using it in Security Rules, use the user ID. See the examples in the doc.

Upvotes: 2

Related Questions