Firestore Query Documents By String Key
I have a firestore db structure like so;
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?
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.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM