Flutter - Firebase Firestore document uid does not exists - flutter

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.

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();

Filtering stream from Cloud Firestore using map field

I have a Cloud Firestore collection called chats and their documents have the following structure:
Here you have another document from the same collection:
As you may seen, both documents have a common users item, in this case the common item is modestovasco#gmail.com: null, but on the first document the item is at first place and on the second item it is at second place.
I need to create a Stream that should filter the chats collection for documents which have a certain email at field users and should filter on both places, first and second.
In the current state, if I am looking for modestovasco#gmail.com, the filter should retrieve both documents.
Here you have my current code for the filter:
FirebaseFirestore.instance
.collection("chats")
.where('users', isGreaterThanOrEqualTo: {usuario!.email: null})
.snapshots()
This stream is only getting the first document.
You can use arrayContainsAny method on where clause it will give you those documents where the given email is present on users field no matter on which place the email is present.
The modified code is given below:
final String emailTobeChecked = 'modestovasco#gmail.com';
FirebaseFirestore.instance
.collection('chats')
.where('users', arrayContainsAny: emailTobeChecked)
.snapshots()
For more information you can click here

Flutter / FireStore : Get the ID of the document just created in Collection

I am adding data in Firestore with User > Accomodation collection. Firestore is automatically creating a new document in the collection Accomodation with all the details I haved added. But now I need to get the id of the document that he has just created to pass it to another screen. Should I create my own Accomodation ID or is there a way to get it ?
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accomodation')
.add(data);
The User can have multiple Accomodation already created, I need to get the one just created.
after adding a document you will get a document reference, with this reference you can get id, i don't know is that method names changed or not after new patches but solution must be similar to that
DocumentReference docRef = await
FirebaseFirestore.instance
.collection('Users')
.doc(uid)
.collection('Accomodation')
.add(data);
print(docRef.documentID);

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.

Flutter : Assigning a collectionGroup to Collection Reference type

Can I assign a CollectionGroup to a Collection Reference like this
to get the parent document of a subcollection.
CollectionReference collectionReference = Firestore.instance.collectionGroup('students');
DocumentReference documentReference = collectionReference.parent();
No, that's not possible. A call to collectionGroup() returns a Query object. A query doesn't have a "parent".
When you execute a collection group query, the matched documents could come from any number of different subcollections with the same name. You won't know anything until you actually look at the DocumentSnapshot objects that come back. If you want to know where one of those documents came from, you ask look at the DocumentSnapshot and use its reference property to find the DocumentReference that describes where it exists. That DocumentReference has a parent that tells you which collection that individual document is in.