How to find Firestore collections which don't have parent documents? - firebase

I'm trying to clean up my Google Cloud Firestore database, and I have some subcollections with no parent doc (the parent was deleted). How can I find all of those, using the Firebase Admin SDK, so I can delete them?

You will end up writing a lot of code for this. I'm going to link to nodejs APIs.
For each collection where there could be missing documents, you will need to need to query that collection with listDocuments(). That will return a list of all documents in the collection, including the missing documents that have subcollections. You will then need to iterate the DocumentReferences returned in that list, and call get() on every one of them. The returned DocumentSnapshot will then tell you if it exists or not using its exists property.
After you have all the DocumentReference objects referring to missing documents, you can then follow the instructions in this other question that describes how to delete all nested subcollections under that DocumentReference, go straight to the Firebase documentation.

Related

What happens to documents and subcollections in a deleted collection?

Let's say that I have a collection with 5 documents and I delete that collection directly from the Firebase Console... do all the documents and nested collections are deleted too?
If the answers is yes... this behavior is only when doing it directly from the Console? Or is it possible from the SDK?
I delete that collection directly from the Firebase Console... do all the documents and nested collections are deleted too?
YES. All their documents and its subcollections will get removed in a click. Please refer the doc here!
this behaviour is only when doing it directly from the Console?
You could do it using Firebase SDK as well, like mentioned below.
is it possible from the SDK?
Yes it is possible; but, you cannot achieve it with single query call. To delete an entire collection or subcollection in Firestore, retrieve all the documents within the collection or subcollection and delete them.
Firestore ref - https://cloud.google.com/firestore/docs/manage-data/delete-data#collections
Yes if you delete a collection all of its documents and nested collection will get deleted. Also, the deletion can be done directly in the console and from the SDK

How to access virtual documents in Firestore through Firebase SDK?

I am a beginner and my firestore database has a collection which has documents and each document has a subcollection without any other fields. I can't find a way to access the ID of the empty document and get the subcollection associated with the doc using the SDK. I could do it on console but I have to too much to do and cannot also make changes to my application for now so I need to use the SDK. Please suggest me some way to access them like the console.
If you know how the CollectionReference of the sub-collection, you can easily get the parent DocumentReference with parent property of the CollectionReference. From the DocumentReference you just need to use the id property.
You didn't share much details on your data model, so it is difficult to give more guidance on how to find the CollectionReference. Mark's answer presents some possible approaches.
So, I used collection group query(collectionGroup) to query the documents in the subcollections(which have the same ids) and found the IDs of the parent document by the parent property.
Huge thanks to #mark carlton and #Renaud Tarnec for these suggestions.
CASE 1: If the subcollections are queried across multiple documents, you could use a collection group query which queries collections of the same name regardless of their position in the hierarchy. This way you don't actually need to know the ID of the document
CASE 2: Use a document ID you will remember for such a case as opposed to having a UID for the ID. This was your queries make much more sense to you

Does Firebase remove collection indexes when a collection becomes empty?

When a collection is deleted from Cloud Firestore, its indexes are deleted along with it. I presume that when a collection goes from one or more documents to zero documents that its indexes are preserved. However, in the Cloud Firestore UI, when a collection goes from one document to zero the collection disappears from the root collections tree. Again, I presume this is an artifact of the Cloud Firestore UI, but it got me wondering whether something more happens when a collection becomes empty (as opposed to the collection being deleted outright).
Can you please help clarify what happens (if anything) when a collection goes from one or more documents to zero in Cloud Firestore? Do I need to be worried about losing any indexes when this occurs?
I'm neither Googler nor Firebaser, BUT...
Firestore indexes documents, not collections - the collection paths are an organizing principle more than physical entities. The "collections" are part of the path to documents, and it's the paths and the document fields that end up indexed.
Case in point: you can actually delete a collection while child documents remain, and they will still be indexed with the collection name/ID as part of their path - you'll see this in the console with the collection (and any interstitial document) names italicized.
When a collection goes from 1 to 0 documents, all that happens is that the document is gone, and nothing else. The UI sees no reason to display a collection when there is nothing to show.
Collections don't really "exist". They are just ways to organize documents for the purpose of making queries. What you see in the console is just there to help you visualize the contents of the database. Collections will apparently spring into "existence" when a document is first created, and just as quickly disappear when there are none. They do not work like directories in a filesystem.
An index is just a way of telling Firestore that you have special query needs for documents in a certain named collection or collection group. The index simply enables the query against the documents in the collection or collection group that you name. The index works without requiring any documents to index, and it will continue working no matter how many documents exist.
Some great answers by LeadDreamer and Doug already, but one more thing you seem to be curious about: deleting all documents from a collection does not affect the index definitions for that collection. So if you later add documents to the collection again, the same index definitions will still apply.

Create a firestore document with a collection

I need to create a firestore doc which also has a collection, ideally in a single write operation.
I'm not seeing anything like this in the documentation, so failing that any tips on getting the created doc id and then adding multiple documents to a collection?
Edit: I'm developing in typescript/js
You can use a transaction or batch write to perform atomic operations among multiple documents. In both cases, you need to know all the IDs for all the documents you want to create ahead of time. You don't need to create a document in order for there to be a subcollection. Documents don't actually "contain" subcollection. Subcollections are currently just a technique for organizing your data.
It wasn't really clear from your question, but if your first document requires a generated ID, you can use the example code in the documentation that generates a DocumentReference object, which you can populate later in your transaction or batch.
Since you didn't say which language or system you're working on, I don't know which code sample to show here from the docs, so you'll have to go to the documentation linked here to see how it works. You'll end up using a method called "doc" or "document" on a CollectionReference to generate the DocumentReference with the generated ID.

Is it possible to check if a document has a subcollection in firestore?

I want to write a recursive function in which I can pass in a document reference and delete that document and it's subcollections. I was thinking that the base case would be that the document has no subcollections. If it has a subcollection, I would pass that subcollection into the recursive function, and delete any existing documents. If not, I would simply delete that document and return. So is there someway I can check if a document has a subcollection, and if so retrieve it?
According to Doug Stevenson here there are no client SDK APIs for listing subcollections of a document.
Although you may find your answer here but that also may create further problems. In documentation also deleting collections from an Android client is not recommended.

Resources