I have a series of nested objects in a collection in firestore, that have a key of a UID. I want to query all documents that relate to a user, by checking if their UID is the object key (basically if it exists). This is the model in firestore:
and this is how I am currently trying to return the documents:
getAllTenancyOffersByUserId() {
return this.afs.collection('collection_name', ref =>
ref.where(`application.members.${this._auth.currentUserId}`, '==', !null),
);
}
but this returns nothing. I understand that I could just query by that status, however it is constantly updating so will cause problems
The following should do the trick:
getAllTenancyOffersByUserId() {
return this.afs.collection('members', ref =>
ref.where(firebase.firestore.FieldPath.documentId(), '==', `${this._auth.currentUserId}`)
);
}
See the corresponding doc: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#.documentId
Actually, what you could do is to filter by any string field that you have in the object like this:
.collection("collectionName")
.where("mapArray.map.stringField", ">", "''")
the important part here is the string field being; ">", "''"
I believe it is not possible to query whether a object exists or not. I think your idea of querying a field inside your object may be the best bet.
What you could try is querying the date_modified field for anything greater than 1900.
getAllTenancyOffersByUserId() {
return this.afs.collection('collection_name', ref =>
ref.where(`
application.members.${this._auth.currentUserId}.date_modified`,
'>=',
new Date('1900-01-01')
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With