Firestore query for subcollections on a deleted document - firebase

When using the Firebase console it is possible to see all documents and collections, even subcollections where the path has "documents" that do not exist.
This is illustrated in the picture included here, and as stated in the docs and on the screenshot as well. These documents won't appear in queries or snapshots. So how does the console find these nested subcollections, when a query does not return them?
Is it possible, somehow, to list these documents. Since the console can do it, it seems there must be a way.
And if it is possible to find these documents, is it possible to create a query that fetches all the documents that are non-existant but limited to those that have a nested subcollection? (Since the set of all non-existant documents would be infinite)

The Admin SDK provides a listDocuments method with this description:
The document references returned may include references to "missing
documents", i.e. document locations that have no document present but
which contain subcollections with documents. Attempting to read such a
document reference (e.g. via .get() or .onSnapshot()) will return a
DocumentSnapshot whose .exists property is false.
Combining this with the example for listing subcollections, you could do something like the following:
// Admin SDK only
let collectionRef = firestore.collection('col');
return collectionRef.listDocuments().then(documentRefs => {
return firestore.getAll(documentRefs);
}).then(documentSnapshots => {
documentSnapshots.forEach(doc => {
if( !doc.exists ) {
console.log(`Found missing document: ${documentSnapshot.id}, getting subcollections`);
doc.getCollections().then(collections => {
collections.forEach(collection => {
console.log('Found subcollection with id:', collection.id);
});
});
}
});
});
Note that the Firebase CLI uses a different approach. Via the REST API, it queries all documents below a given path, without having to know their specific location first. You can see how this works in the recursive delete code here.

Is it possible to create a query that fetches all these subcollections that are nested under a document that does not exist.
Queries in Cloud Firestore are shallow, which means they only get documents from the collection that the query is run against. There is no way in Cloud Firestore to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use properties of documents in a single collection or subcollection.
So in your case, even if one document does not exist (does not contain any properties), you can still query a collection that lives beneath it. With other words, you can query the queue subcollection that exist within -LFNX ... 7UjS document but you cannot query all queue subcollection within all documents. You can query only one subcollection at a time.
Edit:
According to your comment:
I want to find collections that are nested under documents that do not exist.
There is no way to find collections because you cannot query across different collections. You can only query against one. The simplest solution I can think of is to check if a document within your items collection doesn't exist (has no properties) and then create a query (items -> documentId -> queue), and check if has any results.
Edit2:
The Firebase Console is telling you through those document ids shown in italics that those documents 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.
In Cloud Firestore documents and subcollections don't work like filesystem files and directories you're used. 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. With other words, there is no physical document at that location but there is other data under the location.
In Firebase console those document ids are diplayed so you can navigate down the tree and get the subcollections and documents that exist beneath it. But in the same time the console is warning you that those document does not exist, by displaying their ids in italics. So you cannot display or use them because of the simple fact that there is no data beneath it. If you want to correct that, you have to write at least a property that can hold a value. In that way, those documents will hold some data so you can do whatever you want.
P.S. In Cloud Firestore, if you delete a document, its subcollections will continue to exist and this is because of the exact same reason I mentioned above.

Related

Read data from firebase document with sub collections >> sub documents

Is it possible to get data of sub-documents by reading only the main document in Firestore? Basically, the Firebase listCollections() method gives the id of subcollections, but how can we get their data?
I have tried for listCollections() to get the id of sub-collections, but have not found a way to get data sub-collections data.
Is it possible to get data of sub-documents by reading only the main document in Firebase?
In Firestore, the queries are shallow. This means that it can only return documents from the collection that the query is run against. So there is no way you can get documents along with the data that exists inside sub-collections in one go. A single query can only read fields of documents in a single collection.
If you need to get documents from sub-collection, you need to perform separate queries. If you have collections or sub-collections that have the exact same name then you can use a collection group query.
Firebase listCollectionIds(),Firebase listDocuments() are well documented by firebase but nobody use and write on it....
I think the best way is to list subcollection to your document throught the well-know get() and then process a foreach on result snapshot
snapshot.forEach(doc =>{
console.log(doc.id)
console.log(doc.data())
})

Getting documents length 0, but data is present in firestore

I'm building an e-commerce app but when I query firestore to retrieve data I get documents length 0. Data Is present in Firestore. I have no idea what is causing this issue.
I have added all the dependencies.
Firestore.instance
.collection('Products')
.getDocuments().then((value) => print(value.documents.length));
Why this query is not giving me a total number of documents inside Products.
When I run this Query I get a Correct number of documents inside Fruits.
Firestore.instance
.collection('Products')
.document('ABHISHAK FRUITS').collection('Fruits').getDocuments().then((value) => print(value.documents.length));
The log message is correct - your first query returned no documents.
Notice that IDs of the documents in the console are in italics. This means that the document is not present. The reason why the console is showing these missing documents is because the document ID has a subcollection nested under it, and it's letting you click through to browse them.
Your second query using the subcollection works fine because the subcollection has documents present in it.
The bottom line here is that subcollections are not really "contained" within their parent documents like a traditional computer filesystem. The parents documents can be created or deleted without affecting the any subcollections nested under them.

Check if a document exists on Firestore without get() the full document data

So this is possible:
const docSnapshot = await firebase.firestore().collection("SOME_COL").doc("SOME_DOC").get();
console.log(docSnapshot.exists);
But it "downloads" the whole document just to check if it exists. And I'm currently working with some havier documents and I have a script where I just need to know if they exist, but I don't need to download them at that time.
Is there a way to check if a document exist without .get() and avoid downloading the document data?
It seems you are using the JavaScript SDK. With this SDK there isn't any way to only get a subset of the fields of a document.
One of the possible solutions is to maintain another collection with documents that have the same IDs than the main collection documents but which only hold a very small dummy field. You could use a set of Cloud Functions to synchronise the two collections (Documents creation/deletion).
On the other hand, with the Firestore REST API, it is possible, with the get method, to define a DocumentMask which defines a "set of field paths on a document" and is "used to restrict a get operation on a document to a subset of its fields". Depending on your exact use case, this can be an interesting and easier solution.

Fetch all keys from Firestore collection with admin sdk

I need to fetch all the ids/keys of a collection in Cloud Firestore. Currently I do it like this (groovy):
ApiFuture<QuerySnapshot> snapshot = firestoreClient.database.collection(bucket).get()
List<QueryDocumentSnapshot> documents = snapshot.get().getDocuments()
for (QueryDocumentSnapshot document : documents) {
keys.add(document.id)
}
I run this on a collection which potentially has could have a lot of documents lets say 30.000 which causes a java.lang.OutOfMemoryError: Java heap space
The thing is that I don't need all the documents. As seen in my code all I need is to check which documents are in the collection (ie. a list of keys/id's), but I have not found any way to grab them with out fetching all the documents which has a huge overhead.
I using the Java Firebase Admin SDK (6.12.2).
So I'm hoping that there is a way to grab all the keys with out the overhead and without my heap maxing out.
Calling get() will get you the full documents. But you should be able to do an empty selection. From the documentation for select():
public Query select(String... fields)
Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.
So something like this:
firestoreClient.database.collection(bucket).select(new String[0])
Also see:
How to get a list of document IDs in a collection Cloud Firestore?
the Firestore Java reference documentation for the select function

Resolve Firestore References

Firebase Firestore has a reference type while defining fields of a document which allows us to add a reference to another document via its "Document path".
For example, I have the document animals/3OYc0QTbGOTRkhXeiW0t, with a field name having value Zebra. I reference it in the array animals, of document zoo/xmo5wX0MLUEbfFJHvKq6. I am basically storing a list of animals in a zoo, by referring the animals to the corresponding animal document in the animals collections.
Now if I query a specific document from the zoo collection, will references to the animals be automatically resolved? Will I the get the animal names in the query result? If not, how can I achieve this?
All document queries in Firestore are shallow, meaning that you only get one document in return for each document requested.
References in a document are not automatically fetched - you will have to make subsequent queries using the references in the document to get those other documents on your own.
Same thing with documents in subcollections - they require separate queries.

Resources