5

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!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

10

The Firebase Console isn't indicating 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. With 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, 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. With 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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    Thank you very much! this explained all i needed to know! :) – Casper Christensen Nov 08 '18 at 13:43
  • @AlexMamo How would you then delete the subcollections when the document doesn't exist, since it doesn't show up in queries? – eikooc Jan 17 '19 at 19:01
  • @eikooc To delete a subcollection that exists beneath a document, you need to get all documents beneath that subcollection and delete them. You can check **[this](https://stackoverflow.com/questions/49125183/how-to-model-this-structure-to-handle-delete/49125226)** out. – Alex Mamo Jan 18 '19 at 09:35
  • @Alex Mamo I meant in the case of finding the document. Do you know if it's possible to query for those subcollections on a document that doesn't exist? – eikooc Jan 18 '19 at 13:01
  • @eikooc All this questions should be asked in separate question, here on SOF, but FYI, yes of course you can. The document does not exist because it has no properties but you can still query the subcollections beneath it, right? – Alex Mamo Jan 18 '19 at 13:17