Flutter : get parent data of sub-collection from firestore - flutter

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.

Related

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.

how to delete a complete collection including sub-collection in firebase firestore

I am making an application in which i have to make invoice of cars and the pattern i am using to store the invoice is shown in image below
containerInvoice: {
invoiceNo:123,
total:$9000,
container:{
containerNo:911,
containerTotal:$201999
units:{
id:199,
chasis:a829nn,
}
}
}
if i directly delete containerInvoice's document then it is delete successfully but it leave it's sub-collection and sub-sub-collection undelete.
the way i fount to delete whole collection including its child collection is to access these documents by document's id and delete it, but it is a lengthy process.
Is there any way to delete a parent collection and all the child collection deleted automatically
As #danh pointed out, according to the documentation, when you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections.
If you want to delete a document and all the documents within its subcollections, you must do so manually [1].
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them [2].
[1] - https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents
[2] - https://firebase.google.com/docs/firestore/manage-data/delete-data#collections

Cloud Firestore Document Reference

I have 2 collections in cloud firestore and want to use both of them trough one field of my product 'userBids' which I'm using document reference:
Kullanıcılar(Users)
Ürünler(Products)
Problem
I want to reach my user from my 'userBids' field in my product model, but getting error below;
Class 'DocumentReference' has no instance method '[]'.
Receiver: Instance of 'DocumentReference'
Tried calling: )
Text(data[0]['userBids'][index]['user']['nickname']
So how can I reach one collection from another collection in Cloud Firestore in Flutter?
You need separate queries for "nested" documents. You should know that firestore does NOT actually nest documents; it's more an addressing/indexing convention than anything else. A "child" document can exist without any parent collection or document; it is perfectly valid for your database to have a document like:
.doc("collection1/document1/collection2/document2/collection3/document3")
without any of collection1, document1, collection2, or document2 ever existing. In the Firebase Console you'll see the names and ID's of these non-existant collections & documents in italics.

How to use DocumentReference in firestore query

I have a Firestore Database with two collections, one is called "shops" and the other is called "groups".
In the "shops" collection, there are some documents that represent different shops in a city and in the "groups" collection there is a document with a single field that is an array of reference.
In the array, I saved the last 20 shops that were inserted in the "shops" collection.
I don't know how to get the documents by the reference save in the array.
My method needs to return a Stream<List<Shop>>, I need an idea to resolve my problem, I don't want to use a field in shops' document to save the time when it was inserted.

flutter Issue in getting subcollection from document in firebase

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.