I have recently started working on a web-app for managing trucks/trailers/drivers, but I am ready to give up on using React with Firebase because I cannot figure out how to structure my database.
In short, each user connected to the app has it own fleet (only one!). Each such fleet contains many trucks, trailers, and drivers.
This is how I envision a potential database structure:
my database should contain a 'users' collection where all the user accounts will be stored as documents
each document in the above collection should contain a few fields that define the user (name, email, etc), and a 'fleet' sub-collection;
each 'fleet' sub-collection should contain 3 sub-collections: 'drivers', 'trucks', 'trailers'
(FOR CLARIFICATION, THIS IS HOW IT WOULD LOOK: users_collection -> user_doc -> fleet_collection -> (drivers_collection | trucks_collection | trailers_collection)
It all sounds amazing (in my opinion), but there one big issue: Firestore does not allow collection nested directly under collections; this means that I would have to create an empty document under the 'fleet' collection for which then I would be able to create the other 3 sub-collections ('drivers', 'trucks', 'trailers')... this seems very stupid and I would love if that could be avoided.
(FOR CLARIFICATION, THIS IS HOW IT WOULD LOOK: users_collection -> user_doc -> fleet_collection -> fleet_doc -> (drivers_collection | trucks_collection | trailers_collection)
Is there something I am missing in this scenario?
One potential solution I thought of would be to have 'fleet' become it's own top-level collection (same as 'users'); if go this way, then I would simply have to create a new document for each user (same as for the 'users' collection) and inside each such document create the 3 sub-collections ('drivers', 'trucks', 'trailers'). Whilst this would work, I don't find this particularly attractive, as I like the idea of my database being a representation of the real world hierarchy.
(FOR CLARIFICATION, THIS IS HOW IT WOULD LOOK: users_collection -> user_doc; fleet_collection -> user_doc -> (drivers_collection | trucks_collection | trailers_collection) )
I greatly appreciate you reading this and your potential answers! Thank you!
Is there a way to have a collection directly under another collection in Firestore Database?
Nope. The Firestore data model starts with collections directly under the root, then one or more documents under each collections, and optionally one or more collections under each document again (and further nested until the documented maximum). It is not possible to change this model.
I'm not sure if I follow all intricacies of your use-case, but my first thought is that I'd have all cars (so the combines contents of your (drivers_collection | trucks_collection | trailers_collection)) in single subcollection under the fleet document. If you give each car document a cartype
field, you can use that to filter for a specific type.
Keep in mind that the "best" data model for a NoSQL database mostly depends on the exact use-cases of your app. Since we (and likely even you) can't know all use-cases, you will likely have to modify the model as you add/uncover more use-cases.
To learn more about this, I recommend reading NoSQL data modeling techniques and watching Get to know Cloud Firestore.