Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore Query - One to Many Relationship

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
like image 527
AD Mark Avatar asked Aug 27 '18 17:08

AD Mark


People also ask

How do I query multiple values in firestore?

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.

How do you create a one to many relationship in firebase?

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.

How do I query Subcollection in firestore?

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.

How do I get distinct values on firestore?

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.


1 Answers

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.

like image 173
Alex Mamo Avatar answered Sep 22 '22 23:09

Alex Mamo