0
--- Users (Collection)
    |
    --- p0A1fXH4l2TpvGE2lo0x
        |
        --- List (HashMap)
            |
            --- ID (String) (Value: UQx4CWRgnVLOdKEY3AKJ)
            --- NAME (String) (Value: ...)

In Firestore, how can I find the documents that have a list ID equal to UQx4CWRgnVLOdKEY3AKJ? Before deleting the list, I need to remove it from the users who have used it. How can I determine which documents are using this list ID so I can delete them?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Taha Sami
  • 1,565
  • 1
  • 16
  • 43

2 Answers2

1

It looks like your List (HashMap) may have multiple child objects, and you want to search across all of those for a specific ID value.

There is no way to search across all objects in a map field in Firestore. If you want to search across all ID values, add an additional array field with just this values, e.g.

ListIDs: ["UQx4CWRgnVLOdKEY3AKJ", ...]

With that field in place, you can then use the array-contains operator to query for matching documents.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is it possible to delete the List field if the ID inside this field equals a specific ID? [Here is the data structure as an image.](https://i.stack.imgur.com/NJi1g.png) – Taha Sami Feb 09 '23 at 16:11
  • Is it possible to retrieve all documents where the ID inside the list field equals a specific value? – Taha Sami Feb 09 '23 at 16:13
  • 1
    #1) No. You will have to add a top-level array field as shown in my answer. #2) Once you do that, you can use an array membership condition as shown in my answer. – Frank van Puffelen Feb 09 '23 at 17:51
1

If I understand correctly, the List field inside the user document is a Map which contains only String values. So if your database schema looks exactly like this:

db
|
--- Users (collection)
     |
     --- $uid (document)
          |
          --- List (map) //
               |
               --- ID: "UQx4CWRgnVLOdKEY3AKJ"
               |
               --- NAME: "Taha Sami"

To get all users where the ID field within the List holds the value of UQx4CWRgnVLOdKEY3AKJ, a query like this will do the trick:

Query queryByListId = db.collection("Users").whereEqualTo("List.ID", "UQx4CWRgnVLOdKEY3AKJ");
//                                                            
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    Yes that is what i want, I'll try it after 2 hours and tell you, Is that mentioned on the docs? – Taha Sami Feb 10 '23 at 08:32
  • 1
    I'm not sure if this particular type of query is mentioned in the docs. – Alex Mamo Feb 10 '23 at 08:45
  • I'll make your answer the corrected answer because it is working as I want it to, and I'm not sure why Frank van Puffelen didn't inform me of this method. Perhaps he misunderstood me. Thank you, Alex and Frank. – Taha Sami Feb 10 '23 at 11:18
  • What about a hashmap inside an array? If I want to delete an image with the file name `9OoISFCPlA9H0KDkG6yl` from Firebase Storage, I need to first remove it from the documents that use it. [Here is the data structure as an image.](https://i.stack.imgur.com/3zKmR.png) Is that possible? If so, what is the easiest way to do that? – Taha Sami Feb 11 '23 at 08:06
  • I mean, what is the best way to find the documents that use this image and remove it from the array? – Taha Sami Feb 11 '23 at 08:07
  • 1
    I think that this [answer](https://stackoverflow.com/questions/75410860/firebase-how-to-query-through-array-of-objects/75411091#75411091) and this [resource](https://medium.com/firebase-tips-tricks/how-to-update-an-array-of-objects-in-firestore-cdb611a56073) will help. If not, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Feb 11 '23 at 09:12
  • I hope you update your answer and explain how I can do it. I don't need to create a new question since the issue is related to this one. I'll reward you with a 100-point bounty. Thank you, Alex. – Taha Sami Feb 11 '23 at 09:49
  • 1
    You cannot search for documents where an object inside an array contains a field with a specific ID. In that case, you should use the entire object, not partial data. Or use the solution in @FrankvanPuffelen's answer. – Alex Mamo Feb 11 '23 at 13:36