Is possible to check if a collection or sub collection exists?

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:

     .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.

  doc => {
    if (doc.exists) {
        then(sub => {
          if ( > 0) {
            console.log('subcollection exists');

To be more precise:

const querySnapshot = await admin.firestore().collection('users').doc('uid').collection('sub-collection').limit(1).get()
if (querySnapshot.empty) {console.log('sub-collection not existed')}

This is how I was able to check if a collection exists?

I target the document path first, then if it exists, It means the collection afterwards exists and I can access it.

>  db.collection("collection_name").document("doc_name").get()
>        .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
>                     @Override
>                     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
>                         if(task.isSuccessful()){
>                             DocumentSnapshot result = task.getResult();
>                             if(result.exists()){
>                                *//this means the document exist first, hence the 
>                                 //collection afterwards the doc_name will 
>                                 // I can access the collection*  
> db.collection("collection_name").document("doc_name").collection("collection_name2").get()
> .addOnCompleteListener(task1 -> {    if(task1.isSuccessful()){
>       ...
>     } }); } } });

isEmpty property of QuerySnapshot returns true if there are no documents in the QuerySnapshot.

Read More:   Is there any reason to use a synchronous XMLHttpRequest?

Thus you can simply check if isEmpty is true or false.

const subcolRef = collection(db, "parentCollectionTitle", "parentDocId", "subcollectionTitle")
const subcolSnapshot = await getDocs(subcollectionRef)

if (!subcolSnapshot.empty) {
    console.log("subcol does exists!");
} else {
    console.log("subcol does NOT exist!");

(Firebase v9)

This is NextJS (React) code for checking if a sub-collection “history” exists or not in collection “users” > doc>user-Id,
if it exists then take data in history, else keep have-history == false.
you can then use {havehistory?<></>:<></>} for showing different info, as per data.

    const [history, setHistory] = useState([])
const [havehistory, setHavehistory] = useState(false)

  onSnapshot(query(collection(db, "users", user.uid,"history")), (querySnapshot) => {
      const historyBro = => {
        return {, id: };

make sure your imported the required modules. e.g.

import { useState } from "react";

import {db} from '../firebase'

import { collection,doc, query, onSnapshot} from "firebase/firestore";

import Link from "next/link";

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts