1

I have a firestore db structure like so;

Imgur

How can i query all documents under auctions collection that have a given cargoOwnerId?

I am using com.google.firebase:firebase-firestore:20.2.0 in my project.

I have looked through similar issues here on stack overflow but no to success.

I have also read the documentation by Google about doing this here but nothing seems to work when I run the code below

FirebaseFirestore.getInstance()
.collection("auctions")
.whereEqualTo("cargoOwnerId","ZkYu6H6ObiTrFSX5uqNb7lWU7KG3")
.get()
.addOnCompleteListener(task -> {

     Log.d("Logging", "Size: " + task.getResult().size());


});

I expect to return a list of all the documents that contain ZkYu6H6ObiTrFSX5uqNb7lWU7KG3 as the cargoOwnerId but it returns nothing at all. The size is 0.

Am i missing something?

2 Answers 2

1

The way your document is structured, what you're trying to do is not possible. You can't query by map properties when you don't also know the name of the map property. The name of map property would also have to be consistent among all your documents.

At the top level of your document, you apparently have a single field with the same id as the document, which is a map. It's not clear to me at all why you want a single map field in your document. It seems to me that you don't want a single map and instead want all the fields of that map to be document fields instead. This would allow you to perform the query you're asking about. Perhaps you made a mistake in populating the document.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot for point that out. I was using a single map instead of using the fields of the map as the document fields.
0

This is possible.

Try changing task.getResult().size() to just task.size()

Example:

db.collection("auctions")
        .whereEqualTo("cargoOwnerId","ZkYu6H6ObiTrFSX5uqNb7lWU7KG3")
        .get()
        .addOnSuccessListener { documents ->

            Log.d("Logging", "Size: " + documents.size());


            for (document in documents) {
                Log.d(TAG, "${document.id} => ${document.data}")

            }
        }
        .addOnFailureListener { exception ->
            Log.w(TAG, "Error getting documents: ", exception)
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.