How can I implement a condition using boolean operators and and or using firestore?
An example of what I want to do:
this.itemsCollection = this.afs.collection<Message>('messages', ref => {
let query: firebase.default.firestore.CollectionReference | firebase.default.firestore.Query = ref;
query = ((query.where('senderId', '==', this._cs.senderId || '') && query.where('receiverId', '==', this.receiverId || ''))
|| (query.where('receiverId', '==', this._cs.senderId || '') && query.where('senderId', '==', this.receiverId || '')))
return query;
})
this.items = this.itemsCollection.valueChanges();
There is no option to do an OR on different fields in Firestore. You will have to execute multiple queries for this, and then merge the results in your application code.
To check an OR for multiple values on a single field is called an in condition in Firestore, and can be done with:
query.where('senderId', 'in', [this._cs.senderId, ''])
The empty string here must literally exist in the document for the to match the condition. There is no way to filter for documents where senderId doesn't exist, as documents without a value for that field simply won't exist in the index that Firestore created for senderId.