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 subco
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().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');
}
});
}
});
To be more precise:
const querySnapshot = admin.firestore().collection('users').doc('uid').collection('sub-collection').limit(1).get()
if (querySnapshot.empty) {console.log('sub-collection not existed')}