Search code examples
firebasegoogle-cloud-firestore

Firestore document/subcollection is not existing


I have this db structure:

Firestore-root
|
--- sites (collection)
|     |
|     --- common (document)
|            |
|            --- //templates (collection)
|            |
|            --- //menus (collection)
|            |
|            --- //articles (collection) <----
|     --- other (document)
|            |
|            --- //articles (collection)

When I try to add articles to the db (shown by the arrow) the "common" and the "other" - document is in italic and therefore doesn't exist.

My code when i try to add: priority is common, type is articles.

def documentReference = firestoreClient.databaseReference.collection(siteName).document(priority).collection(TYPE).document(key)
documentReference.set(article)

this is the console: firestore console image

Is this a bad way to structure my db or is there a quick fix?

I have tried to create the sub collections first but without any luck!


Solution

  • The Firebase Console isn't indicating to you that the "common" and the "other" documents are deleted. It is telling you that it just does not exist. Those documents do not exist because you didn't create them at all. What you did do, was only to create a subcollection under a document that never existed in the first place. In other words, it merely "reserves" an ID for a document in that collection and then creates a subcollection under it. Typically, you should only create subcollections of documents that actually do exist but this is how it looks like when the document doesn't exist.

    One thing to remember is that in Cloud Firestore documents and subcollections don't work like filesystem files and directories. If you create a subcollection under a document, it doesn't implicitly create any parent documents. Subcollections are not tied in any way to a parent document.

    Document IDs shown in italics are not necessarily "deleted". They are shown that way because the document does not exist. In other words, there is no physical document at that location but there is other data under the location.

    If you want to correct that, you have to write at least a property that can hold a value.

    P.S. In Firestore, if you delete a document, its subcollections still exist.