As an instance think of an e-commerce app where a user can upload as many as items and those items have categories and subcategories.
How to set user_id as a foreign key in each items posted as well as item_id in users collection in order to query all the items posted by a specific user.
How do we make use of reference data type in Firestore to set foreign keys?
Are there any other methods to set foreign keys?
Database Structure:
Users/doc_id/name..
Categories/fashion/clothing/auto_doc_id/type:denims
in order to query all the items posted by a specific user. To achieve this, there is no need to set user_id as a foreign key in each items posted as well as item_id in users collection. You can only add a new property to the item object named postedBy which will hold as a value the user id.
Each document contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents. All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.
To add Document with Custom ID to Firestore with JavaScript, we call the set method. db. collection("cities"). doc("LA").
in order to query all the items posted by a specific user.
To achieve this, there is no need to set user_id as a foreign key in each items posted as well as item_id in users collection. You can only add a new property to the item object named postedBy
which will hold as a value the user id. To display only the items of a specific user you need to query the database using the following code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference itemsRef = rootRef.collection("items");
Query query = itemsRef.whereEqualTo("postedBy", user_id);
There is also another approach in which involves duplicating data. There is no problem with this tehnique, when it comes to Firebase. This is a quite common practice, which is named denormalization
and for that, I recommend you see this video, Denormalization is normal with the Firebase Database. This is for Firebase Realtime database but the concept applies in the same way also to Cloud Firestore.
So with other words, you can create a new collection in which you can store all items that were added by a specific user. The database structure should look like this:
Firestore-root
|
--- users (collecton)
|
--- userId (document)
|
--- userItems (collecton)
|
--- itemId (document)
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
If you want to have a reference to another table with a foreign key, you should store for example the user id with the data you want to work, for example, let's say you have this structure
This is from my project, I have for example a node that is likes, each like is a push key, and inside that pushkey I have the userID of each user who liked a post, the post is represented with the pushKey
Now, I have another node that is called users, and inside you can see my userID
As you can see, I have the same userID in my node users that is in my likes node.
So what I'd do is this
This is how I use a foreign key like the userID to work with 2 different nodes
Now, let's say I want to brinf up all the likes from one photo, I just loop through my likes node and each push key is a photo, so I do the same process and get all the photos and likes of each one. So if I get all the likes of the photos I'm also getting the userID who liked, because it's inside my likes node, and then with that I can just reach to the users node and get all the data from the user who liked.
PS: there is not special methods or something to create foreign keys, it depends on your structure and how you will build your app. You set your fk by yourself thinking on how to merge two tables and use the info from both, in this case two nodes that interact with each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With