The picture below shows the data structure for the simple chat application. A channel is created between two users when they want to communicate. Member string array field lists the usernames.
I want to notify the user when any of the channels of logged in user receive a message. Not sure how this can be done.
I tried below query with no success. Any ideas?
listenerRegistration2 =
firebaseFirestore.collection(COLLECTION_CHANNELS)
.whereEqualTo("members.0", username)
.addSnapshotListener((queryDocumentSnapshots, e) -> {
Log.d("myApp", "queryDocumentSnapshots = ");
});
Edit: September 12, 2018
Starting with
August 28, 2018
, now it's possible to update array members. More informations here.As in the official documentation regarding arrays:
So unfortunately you cannot achieve this using arrays. If you only want to get the entire
memebers
array, just use aget()
call and then only to iterate over aMap
like this:But note, even if
memebers
object is stored in the database as an array,entry.getValue()
returns anArrayList
, not anarray
.There is also an workaround here, that can help you achieve this but you need to change the
memebers
array property to amap
. So a better approach will be if you consider this alternative database structure, where each memeber is the key in a map and all values are true: