Hussam
Hussam

Reputation: 1696

Different collections for each type of user in root of Firestore

I am creating an app based on Cloud Firestore. Apps working will somehow be same as uber. So the problem is as I am fairly new to NoSQL databases, I am having problems creating a good structure for the database. Here is the LINK to the UML that I planned to use for my Android App. I am planning to use Firestore for storing information about the users and other data whereas real-time database to be used for real-time map updates.

My main concern here is, as you can see in the UML, it somehow show relationships among different tables but NoSQL has no tables and relations which is giving me hard time here. After around 4 hours of continuous internet searching about this I am still not sure what to do. I have some questions here that I'd like to be answered:

1) Is there any way I can create relations in between different collections ?

2) Should I save all 3 type of users in same collection "user" or make different collection for each type in the root. What would be the consequences of using both approaches.

If I use first approach what can I do to distinguish between each type of user.

3)How much of the deep nesting I am allowed to do in a single collection ie sub-collections in collections ?

4)Is my decision to use 2 different database in the same apps right?

5)Is firebase the right choice for an app that I am creating?

The questions might sound too beginner because I am a just learning. Any help would be appreciated. Any links to some useful documentations, articles, or guides would be appreciated too.

Upvotes: 2

Views: 2076

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138924

1) Is there any way I can create relations between different collections ?

Yes there is. You can create relations between different collections by holding references as seen in the official documentation regarding supported data types. For example:

projects/[PROJECT_ID]/databases/[DATABASE_ID]/documents/[DOCUMENT_PATH].

There is also another workaround which involves duplicating data and for that I recommend to see the this video, Denormalization is normal with the Firebase Database. Is for Firebase real-time database but same principles applies to Cloud Firestore.

2) Should I save all 3 type of users in same collection "user" or make different collection for each type in the root. What would be the consequences of using both approaches.

For the simplicity, you can use a single collection and add the type of the user as a property like this:

Firestore-root
   |
   --- users
        |
        --- uid
        |    |
        |    --- userType: "admin"
        |    |
        |    --- //other user details
        |
        --- uid
        |    |
        |    --- userType: "driver"
        |    |
        |    --- //other user details
        |
        --- uid
             |
             --- userType: "rider"
             |
             --- //other user details

To query all the users of a particular user type, you can use the following query:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("users").whereEqualTo("userType", admin);

3)How much of the deep nesting I am allowed to do in a single collection ie sub-collections in collections ?

You can chain in depth up to a maximum of 100 subcollections. As per the offial documentation regarding usage and limits:

Maximum depth of subcollections: 100

You may wonder, is this a problem?

As far as I know, Firestore can as quickly look up a node at level 1 as it can at level 100. So for a database as yours, depth should not be a factor that affects speed on a technical level.

4)Is my decision to use 2 different database in the same apps right?

Yes it is. Actually, is a quite common practice. According to both price plans, you can choose which one is more appropriate to your needs.

5)Is firebase the right choice for an app that I am creating?

Questions asking us to recommend or find a book, tool, software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam but in my opinion, yes it is.

Upvotes: 7

Related Questions