skip to Main Content
--- 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?

2

Answers


  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.

    Login or Signup to reply.
  2. 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");
    //                                                            👆
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search