Trying to fetch the all documents present in Users Collections but receiving empty list and when i tried to fetch Documents from Admins collections its working fine.
this._fs
.collection(FBMainCollection.Users)
.get()
.subscribe((usr) => {
const data = usr.docs.map((e) => e.data());
console.log(data);
});
Response
[]
empty list
Unable to fetch documents from a few collections.
working only for collections which doesn`t have nested collections;
Your screenshot shows that you don't have any documents in the Users collection. Notice how all of the document IDs are in italics, and none of them have any fields. That means there is no document there, but there is a nested subcollection under it that you can navigate into. Queries do not read any of the nested subcollections, so instead it just shows and empty collection..
See also:
Firestore document/subcollection is not existing
Why are non auto-generated document Ids are in italics in Firestore console?
Related
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())
})
Is there any way to retrieve a collection along with its sub-collections from Firebase Firestore using Flutter?
fetchTestSite() async {
return await FirebaseFirestore.instance
.collection('sites')
.doc('4R3aOMBFTjumYCbETDU8')
.get()
.then((DocumentSnapshot doc) {
print('document: ${doc.data()}');
});
}
This code snippet only returns the main collection without the existing sub-collections
Is there any way to retrieve a collection along with its sub-collections from Firebase Firestore using Flutter?
No, there is no way you can do that. Queries in Firestore are shallow. This means that it can only return documents from the collection that the query is run against.
There is no way to get documents from a top-level collection and sub-collections in a single query. You can only get documents from a single collection.
However, there is a "hack" that can be made. If you want to get the data from the sub-collection that corresponds to a specific document, you can use a collectionGroup to get documents under a certain path. I explained in the following article:
How to query collections in Firestore under a certain path?
How you can do that and what limitations you might have.
If you consider at some point in time try using the Firebase Realtime Database, what you are looking for it's possible, because when you attach a listener on a specific node, you download all the data beneath it.
This is my data structure. Here are the list of users documents.
and here is the subcollection in every document.
The subcollection is containing strongly typed document which is form.
What I want is to fetch form document (not for the current user) from subcollection workerField from every users document.
what is the implementation method.
If you want to load documents from all workerField collections you can use a so-called collection group query.
So something like:
var docs = await FirebaseFirestore.instance
.collectionGroup('workerField')
.snapshots();
See my answer here for another example: Is wildcard possible in Flutter Firestore query? and more from these search results.
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.
I have this code:
firebase.firestore().collection('items').get()
.then(snapshot => {
Alert.alert(JSON.stringify(snapshot._docs));
snapshot.forEach(doc => {
Alert.alert(doc.id, '=>', doc.data());
});
})
.catch(err => {
Alert.alert('Error getting documents', err);
});
The Alert.alert(JSON.stringify(snapshot._docs)) returns an empty array - so it is not finding any documents. There is one document in this collection:
Is it because I am using an email address for the document id?
UPDATE
An image to show the rest of my database structure:
I am trying to search through document fields under PxlmyvjklhTOADngsSQg under userItems (and other objects that would go there).
UPDATE
This is essentially what I am trying to do, only in theory I would want to use a wildcard for .doc(this.state.user.email):
firebase.firestore().collection('items').doc(**wildcard**).collection('userItems').where("barcode", "==", this.state.text)
But I know that isn't possible with firebase, so I am trying to get all of the items and then parse through them after a successful retrieval.
First of all, you are reaching into the snapshot object using private properties:
snapshot._docs
That's not the right way to do it. The snapshot you get back from a query is a QuerySnapshot object, and you should use the API docs to understand how to deal with it. If you want an array of docs from the snapshot, use its docs property.
Second, your screenshot is actually showing that there are no documents int the items collection. Notice that the document identified by the email address is in italics. That means it's not actually a document - it's just a "phantom" document that you're using to organize a subcollection underneath it called userItems.
When you query a collection, you only get documents that are immediately in that collection, and none of their documents subcollections. Queries are said to be "shallow" in this way. Try creating an actual document with fields immediately in items, then query the collection.