简体   繁体   中英

Problems with Google Firestore collectionGroup - whereArrayContains

I'm working on an Android App written in Java. I'm trying to query for documents in Cloud Firestore using collectionGroup and whereArrayContains .

My structure in Cloud Firestore looks like this:

Teams (collection)
  - teamUUID (document)
  - reservations(collection)
     - reservationDate (document)
     - places (collection)
         - userUUIDs (array of strings) 
               -> one entry ->"vQn9vbWzTtcsB71hgPBhX2uWBuI3"
         - placeID (document)

partial screenshot of the structure

I want to get all documents in the collection places , where the userUUIDs field contains a specific string. My code for this is the following:

db.collectionGroup("places")
                    .whereArrayContains("userUUIDs", 
                       "vQn9vbWzTtcsB71hgPBhX2uWBuI3")
                    .get()
                    .addOnSuccessListener(queryDocumentSnapshots -> {
                           Log.d(TAG, String.valueOf(queryDocumentSnapshots.getDocuments().size()));
                           if (queryDocumentSnapshots.isEmpty()) {
                               //nothing found
                               Log.d(TAG, "nothing found for this user");

If this code is executed, the query is successful, but no documents are returned. If I leave out.whereArrayContains("userUUIDs", "vQn9vbWzTtcsB71hgPBhX2uWBuI3"), one document is returned.

Google Firestore automatically created the index to query for "userUUIDs".

index created in firestore

Why are there no documents returned using whereArrayContains ?

Edit: The problem seams to exist with all queries. I added a string testValue to places and made a whereEqualTo Query. The result was the same. After creating an index (automatically via link in the console) the onSuccessListener code was executed, but no document was found.

Thanks in advance.

Why are there no documents returned using whereArrayContains?

Because you most probably didn't create the correct index. If the userUUIDs exists within the documents from the "places" collection, when running the app you'll find a message like this in the logcat:

Caused by: io.grpc.StatusException: FAILED_PRECONDITION: The query requires a COLLECTION_GROUP_CONTAINS index for collection places and field userUUIDs. You can create it here: https://console.firebase.google.com/v1/r/project/.. .

You can simply click on that link or copy, and paste the URL into a web browser and your index will be created automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM