flutter Issue in getting subcollection from document in firebase - flutter

I have an issue, I can't get subcollection from the document:
I have firestore db with
collection: collectionName
Inside collection documents and inside one document subcollection "items"
collectionName
Document1
Items <- subcollection
Item1
Item2
I use the following code but the items subcollection can't be retrieved from my db....`
```
QuerySnapshot snapshot = await Firestore.instance
.collection('collectionName')
.orderBy("order")
.getDocuments();
```
`then`
```
snapshot.documents.forEach((document) {})
```
The document doesn't have the items subcollection

You can not access a sub-collection like that. Last year, the team working at firestore released a new feature called the Collection Group. If you want to access the items collections try doing this:
Firestore.instance.collectionGroup('items')
This will contain all documents in the items sub-collection regardless of how deeply nested it is. Take a look at this for more information on collection groups.

Related

Query Document to get snapshots from specific collections in Firestore in Flutter

I am using Firebase Cloud Firestore as the database for my app. The data structure is as below
expenses (collection)
|____[userId] (doc)
|___[year-month] collection
|___[expenseId] doc
|______expense fields(amount, date)
How can I get all expenses from a [userId] doc for specific [year-month] collections.
Is there a way to query a doc like we have .where for collection
You have nested collections in your database, right? you can simply chain collection and doc until you get the specific target, since it's not clear what is code variables and what is plain text names, I will assume that all documents and collections names are text, just to so you get the idea:
// This is the reference of a the "year-month" expenses of the user.
Query query = FirebaseFirestore.instance.collection('expenses').doc("userId").collection("year-month");
this will get you a QuerySnapshot containing all documents inside the collection with that "year-month" :
QuerySnapshot = await query.get()
You can also get a specific document in that collection like this:
DocumentReference doc = await query.doc("expenseId").get();

Flutter - Firebase Firestore document uid does not exists

My db structure is like this:
domain -> user uid -> user data
if I try to add data like this:
await FirebaseFirestore.instance
.collection(path)
.doc(firebaseUser.uid)
.collection(collectionName)
.add({...});
where path is the user domain, the database shows like this:
telling me that The document does not exists, it will not appear in queries or shapshots. But if I add the same data by auto id like this:
await FirebaseFirestore.instance
.collection(path)
.add({...});
it works like the second document in picture. Why is this happening?
Look at how the documents in this collection are displayed in an italic font in the Firestore console: This means that these documents are only present as "container" of one or more sub-collection but that they are not "genuine" documents.
As a matter of fact by doing
await FirebaseFirestore.instance
.collection(path)
.doc(firebaseUser.uid)
.collection(collectionName)
.add({...});
You create a doc in the collectionName (sub)collection but not in the path collection.
On the other hand, with
await FirebaseFirestore.instance
.collection(path)
.add({...});
you do create a doc in the path collection.
So if you need to have a document in the path collection AND in the collectionName (sub)collection you need to create these two documents and not only the "child" one.
DETAILED EXPLANATIONS:
Let's take the example of a doc1 document under the col1 collection
col1/doc1/
and another one subDoc1 under the subCol1 (sub-)collection
col1/doc1/subCol1/subDoc1
Actually, from a technical perspective, they are not at all relating to each other. They just share a part of their paths but nothing else.
You can very well create subDoc1 without creating doc1.
A side effect of this is that if you delete a document, its sub-collection(s) still exist. Again, the subcollection docs are not really linked to the parent document.

Flutter firestore query on document array field

I've got this firestore database.
There is a 'users' collection containing documents of users. Each user has their own information such as name and a cart. I would like to get all the 'carts' of a particular user in a stream. How would you go about with the query?
await FirebaseFirestore.instance.Collection("users").doc("docIdHere").get().then((.
document) {
//it will get the first item in the cart with index zero
document.get("cart")[0];
//then do what you want
});

Query firebase docs by subcollection value

I have a Firebase Firestore DB with this structure:
Users (collection) -> user (doc) -> Reports (subcollection) -> report (doc) -> isHandled (bool field).
In my Flutter app, I want to query Users collection, and get only the user docs in which any of their reports collection docs is not handled (isHandled field == false).
I've tried to use Firestore's collectionGroup, but it returns only 1 report doc, instead of many user docs:
await FirebaseFirestore.instance
.collectionGroup('Reports')
.where(
'isHandled',
isEqualTo: false,
)
.get();
Does Firebase Firestore support any query of a collection by its docs' subcollection values?
Thank you very much in advance!
Does Firebase Firestore support any query of a collection by its docs' subcollection values?
No. When you query a collection or subcollection, it will only consider documents immediately within it. The data from a single collection's documents is managed by an "index" - queries only use that index to rapidly find data at scale.
Collection group queries are special in that they consider all collections with the same name, but they will still not consider nested subcollections.
You could perhaps combine a collection group query with further queries to get the "parent" user document from a matched report.

Flutter : get parent data of sub-collection from firestore

I needed to get the parent document data of subcollection from firestore. I used the collection group query to get the sub-collection.
.parent() will do from a document reference or a collection reference.