Is there a way to check if a sub collection exists in firestore for nodejs?
Currently I am using doc.exists
for documents but I need to check if a subcolletion exists within a document in order to write some data or not.
Mateus' Answer didn't help me. Probably it has been changed over the time.
.collection(..).get()
returns a QuerySnapshot which has the property size
, so I just did:
admin.firestore
.collection('users')
.doc('uid')
.collection('sub-collection')
.limit(1)
.get()
.then(query => query.size);
Yes, there is. You can use docs.length to know if the subcollection exists.
I made a sample to guide you, hope it helps.
this.db.collection('users').doc('uid')
.get().limit(1).then(
doc => {
if (doc.exists) {
this.db.collection('users').doc('uid').collection('friendsSubcollection').get().
then(sub => {
if (sub.docs.length > 0) {
console.log('subcollection exists');
}
});
}
});
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