I have field users
in document and this field contains two element in array. I have to check specific two values are in this array.
First, I used
There is no way Firestore in which you can query the database based on an index of an element that exist within an array. It's true that you cannot chain more than one array-contains
calls in a query but there is another workaround that can help you achieve the same thing. So a change is needed in your database structure. So instead of using an array you can use a map and your schema should look similar to this:
Firestore-root
|
--- messages (collection)
|
--- users (map)
|
--- user1ID: true
|
--- user2ID: true
Now a query like this will work perfectly fine:
QuerySnapshot querySnapshot = await sl.get<FirebaseAPI>().getFirestore()
.collection('messages')
.where('users.user1ID',isEqualTo: true)
.where('users.user2ID',isEqualTo: true)
.getDocuments();