I'm implementing chat using Firestore. Here is the structure of Firestore:
|- Chats (collection)
|-- AutoID (document)
|--- user1ID (String)
|--- user2ID (String)
|--- thread (collection)
... and then thread has further fields.
In order to get chat between two users I'm fetching it like:
let db = Firestore.firestore().collection("Chats")
.whereField("user1ID", isEqualTo: Auth.auth().currentUser?.uid!)
.whereField("user2ID", isEqualTo: user2UID!)
It works fine if user 1 is the current user otherwise if I open chat from other account and current user is user 2 it doesn't fetch this document.
Upon searching I found that I can user arrayContains
. So I made an array instead called users
and in it I've added both these IDs. So now the structure is:
|- Chats (collection)
|-- AutoID (document)
|--- users (Array)
|---- 0: user1ID (String)
|---- 1: user2ID (String)
|--- thread (collection)
... and then thread has further fields.
But when I do:
let db2 = Firestore.firestore().collection("Chats")
.whereField("users", arrayContains: Auth.auth().currentUser?.uid!)
.whereField("users", arrayContains: user2UID!)
It's going to fetch the first document it found that has currentUser.uid
(Haven't tested it, I'm saying this based on the documentation I've read).
So, how can I get this chat, if an array contains both id's?