2

My Firestore database structure looks like this (as shown in image)enter image description here

I have a users collection which has a posts sub-collection.

I want to fetch(query)all the posts by country(field of parent collection). Edited: I have other's fields too along with country field, so I don't want to duplicate fields in posts sub-collection.

Is there any solution without any duplication of fields.

My code is below:

 val query: Query =
            postsRef
                .whereEqualTo("country", "USA"). // field from parent document
                .orderBy("createdAt", Query.Direction.DESCENDING)
                .limit(20)
Fortray
  • 133
  • 1
  • 1
  • 8

2 Answers2

2

There is no way you can filter documents in a sub-collection based on the field that exists in the parent document. The best option that you have is to add the country field inside each post and use a collection group query like this:

val query = db.collectionGroup("posts")
                .whereEqualTo("country", "USA")
                .orderBy("createdAt", Query.Direction.DESCENDING)
                .limit(20)

In this way, you'll be able to get only the post from the USA. If you however need to query collections in Firestore under a certain path, please check the following article:

P.S. Please also don't forget to create an index.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I have multiple fields for query, not only country. Is this the right way to duplicate fields that is part of parent document? – Fortray Apr 11 '22 at 13:43
  • Yes, this practice is called [denormalization](https://stackoverflow.com/questions/54258303/what-is-denormalization-in-firebase-cloud-firestore) and it's a quite common practice when it comes to NoSQL databases. – Alex Mamo Apr 11 '22 at 13:46
  • If it's the best way, Is it good to use posts as sub-collection or posts as collection referencing to the userId? Also, How you manage change in field content? I have to update multiple documents. – Fortray Apr 11 '22 at 13:57
  • 1
    We are usually structuring a Firestore database according to the queries that we want to perform. So the "best way" is the one that satisfies your queries. In your case, you have to choose which is better for your queries. – Alex Mamo Apr 11 '22 at 14:00
0

You'll have to first query users in that country and then fetch their posts or alternatively store the country in post document as you've mentioned in your question.

Duplicating a single field "country" should do it as Alex mentioned in his answer. If you need to query based on multiple fields in user's doc then first method can work too

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • I have other field too to store in users collection document. There will be duplication of field if I store in posts sub-collection document. – Fortray Apr 11 '22 at 13:40
  • @Fortray but you'll be querying by country only right? Then you just need to duplicate the country field. If you need to query based on multiple fields in user's document tune ID recommend using first method – Dharmaraj Apr 11 '22 at 13:41