Recently, I decided to migrate my Firebase database to Cloud Firestore. I assumed that it is gonna be a huge improvement on Realtime database but I am afraid it is not. Anyways, I came to a situation in which I want to be able to filter the elements in a sub-collection of a document.
My structure is as follows:
Mailboxes: [
{
id: vhR4bfqyo9Uqn2ONbgPv,
address: "Address 1",
packages: [
{
id: qyo9Uqn2vhR4bfqy,
value: "0011"
},
{
id: 9Uqn2qyovqyhR4bf,
value: "0022"
}
]
},
{
id: BBR4bfqyo9Uqn2ONbEpn,
address: "Address 2",
packages: [
{
id: qn2vhR4bfqyqyo9U,
value: "0066"
}
]
}
]
Imagine I have the value of a package and I want to be able to see if this package exists in mailbox with id vhR4bfqyo9Uqn2ONbgPv.
What I've tried so far is the following (and some variations of it):
db.collection('Mailboxes').doc('vhR4bfqyo9Uqn2ONbgPv').listCollections().then(collections => {
let collRef = collections.find("id", "==", "packages");
// And, from here on, I assume I will be able to filter the items in the collection somehow.
return 0;
});
But, then I get an error in my Firebase cloud function saying:
TypeError: id is not a function at Array.find (native) at db.collection.doc.listCollections.then.collections