36

So few day ago I moved my apps posts to cloud firestore from realtime database because of the more flexable querying that firestore apparently has. I have been modifying my code to work with firestore as the new database. Now I ran into a problem. How can I retrieve every document within "Posts" collection that has "Likes" collection that contains a specifically named document? I don't know if that makes sense so here's an image showcasing the problem:

enter image description here

I had a working query for firebase realtime database but can't figure out how to do it with firestore. Is it possible or do I have to change the structure of my database? If I do have to change the structure, what would be the most convenient way of getting the same result?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aapo Vainonen
  • 423
  • 1
  • 4
  • 8
  • There are very clear document for query. You have to search [here](https://firebase.google.com/docs/firestore/query-data/get-data) – BlackBlind May 17 '19 at 14:22
  • 1
    Yes, I did read everything I could find but didn't figure it out. – Aapo Vainonen May 17 '19 at 14:24
  • @AapoVainonen What do you mean by "that contains a specificaly named document"? The doc in the Likes collection has a specific field value? A specific doc id? – Renaud Tarnec May 17 '19 at 14:33
  • What I mean by that is for example how to retreive every post that specific user has liked. So in this example check if "Likes" collection contains that users user id that we want to see the liked posts of. Sorry if that doesn't make sense english isn't my native language. – Aapo Vainonen May 17 '19 at 15:09
  • So in the document `yQ6m2...` (in the `Likes` collection) there is a field named for example `liker`and you want to search for the docs that have a certain value for this field. right? – Renaud Tarnec May 17 '19 at 15:16
  • Yes. Inside yQ6m2... I have a field called yQ6m2... that has value of true. And I want to check which posts have "Likes" collection that contains document called yQ6m2... – Aapo Vainonen May 17 '19 at 15:20
  • @AapoVainonen Check also **[this](https://stackoverflow.com/questions/56149601/firestore-collection-group-query-on-documentid/56150650#56150650)** out. – Alex Mamo May 20 '19 at 10:24

1 Answers1

38

This is currently not possible with collection group queries. You might be tempted to put a filter on the query for FieldPath.documentId() == 'your-doucment-id', but that just isn't supported. FieldPath.documentId() is actually a token that refers to to fully unique document id, which includes the entire path of the document.

The workaround is to store the document ID as a field in the document, and filter on the contents of that field instead. So, if the document had a field called id with the ID of the document, you could query for collectionGroup("Likes").whereEquals('id', 'your-document-id').

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    I can't find such thing as collectionGroup? – Aapo Vainonen May 19 '19 at 14:57
  • @AapoVainonen Visit https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query and scroll down to the second code block in that section. But I think it might be better to store the Likes as a separate table in this case (on the same level as Posts) and do collection('Likes').doc(LIKE_ID). – Zoltán Matók Oct 23 '19 at 18:26
  • Hi @Doug Stevenson, look likes we can not use `FieldPath.documentId()` with `in`, right? ex: db.collection("events).where("id", "in", [1,2,3]) – Mi.HTR Dec 20 '19 at 04:11
  • 2
    `.where(FieldPath.documentId, whereIn: idList)` only works with `collection`, not `collectionGroup`. Also keep in mind that the list is [limited to 10 values](https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any) using this approach. – AWhitford Apr 21 '20 at 01:01
  • @AWhitford As I said in my answer, that's not supported. – Doug Stevenson Apr 21 '20 at 01:07
  • 18
    most answers are "sorry its not possible in firestore" – ghufranne Jul 18 '20 at 08:48
  • @ghufranne Like all databases, Firestore has distinct strengths and limitations. It can't do everything, and wasn't designed to do everything. In order to take advantage of its strengths, it's necessary to be aware of and work around its limitations. I've written an entire post about that. https://medium.com/firebase-developers/the-top-10-things-to-know-about-firestore-when-choosing-a-database-for-your-app-a3b71b80d979 – Doug Stevenson Jul 18 '20 at 15:17
  • Ouch! I feel like that should be explicitly stated – lenz Nov 03 '20 at 20:22
  • @lenz You can send feedback on every page of Firebase documentation using the button at the top of the page. – Doug Stevenson Nov 03 '20 at 20:23
  • 1
    I tried to use the work around. I get "Property 'whereEquals' does not exist on type 'CollectionGroup'.ts(2339)". Maybe it should be, "db.collectionGroup("Cart").where('id','==', id).get()" – Aaron Tomlinson Apr 30 '21 at 12:33