Firestore Db Model, Attribute In Document Or Subcollection
Solution 1:
There is no right way for a database structure. The right solution for your database, is the solution that fits your needs and makes your job easier. My opinion regarding your database schema is related actually on what you really want to achieve.
If you want to query your database to get only the clients that correspond to a particular admin, then you can go ahead with the first option. If you want at some point to get all the clients from your database, then the second option will help you achieve this. So it might be an option to use both options.
But, if you want to achieve the same thing using the second option, this can be possible only if you'll use a query that is based on a single property (the uid
property) like this:
FirebaseFirestorerootRef= FirebaseFirestore.getInstance();
CollectionReferenceclientsRef= rootRef.collection("Clients");
Queryquery= clientsRef.whereEqualTo("uid", uid);
But if you want instend to achieve the same thing as in the first option and use a query that looks like this:
Queryquery= clientsRef.whereEqualTo("uid", uid).orderBy("aProperty", Query.Direction.ASCENDING);
You need to know that you cannot achieve this. This is not possible because in this case you need to create an index and you cannot create manually an index for each uid
.
One more thing, I recommend you to use the uid
instead of an email address
as the document key.
Post a Comment for "Firestore Db Model, Attribute In Document Or Subcollection"