I am going to develop android mobile app and going to use Firebase as backend. I am new to NoSql Firestore.
In my app an user has many Topics. A Topic has many posts and the post has many comments & one image. Also user has many followers.
In this scenario, how can I design Firestore database for my requirement and what is the best practice? How to reduce no.of read ?
Is following structure OK for my requirement?
Firestore-root
|
--- Topic (collections)
| |
| --- topicId (document)
| |
| --- topicId: "345"
| |
| --- title: "Topic Title"
| |
| --- viewCount: 20
| |
| --- likeCount: 30
| |
| --- Posts (colletion)
| | |
| | --- postId
| | |
| | ---postId: "321"
| | |
| | ---Title: "My title"
| | |
| | --- Image: ' '
| | |
| | --- commentsCount: 1
| | |
| | --- comments (colletion)
| | |
| | --- commentId
| | |
| | ---commentId: "123"
| | |
| | ---comment: "My Comment"
| | |
| --- likes (colletion)
| | |
| | --- likeId (document)
| | |
| | --- userId: true
| |
|
|
--- Users (collections)
| |
| --- userId (document)
| |
| --- userId: "345"
| |
| --- name: "Test Name"
| |
| --- followersCount: 20
| |
| --- followingCount: 30
With the in query, you can query a specific field for multiple values (up to 10) in a single query. You do this by passing a list containing all the values you want to search for, and Cloud Firestore will match any document whose field equals one of those values.
Each Job can contain many Items and one Item must be contained by a single job (Simple One to Many). My present database structure is that both Item and Job are top level lists in firebase and each Item contains the jobKey to which it belongs. This allows me to find all items in a job as long as I know the job key.
You can either go to Firestore Databases > Indexes console and do it, or use the firebase cli. But the easiest option is to just let your code (that performs the query) run and firestore will automatically error out when an index is missing.
There is no specific API to retrieve unique values from Cloud Firestore. You will have to retrieve all relevant documents and determine the unique names in your own code. Alternatively, consider adding a document with unique names and update that with every write.
As a short an answer, yes, looks good to me for your particular requirements. I already answered a question earlier today, which is almost as yours and which I have provided a similar database schema as you already have. But you need to know that there is no perfect solution for structuring a Cloud Firestore database. The best solution, is the solution that fits your needs and makes your job easier. But regarding this structure, I see that you can easely query to get all necessary data, so in my opinion, go ahead with that.
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