Ad

Firestore Query Documents By String Key

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?

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad