This question already has an answer here:
When fetching document from Firestore, am I also getting the collections inside this document?
(1 answer)
Closed 1 year ago.
I have a subcollection "Ratings" inside this "Posts" collection so when i get any post will this collection will be fetched automatically or i have to reference it separately. And if this subcollection Ratings is fetched with the post so it'll be count as a separate read or same as the Post read?
final CollectionReference postsCollection =
FirebaseFirestore.instance.collection("posts");
response = await FirebaseFirestore.instance
.collection("posts")
.get();
Getting a collection will NOT get the subcollections. The subcollections need a separate read.... and reading collection or subcollection considered read.
Related
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?
This question already has answers here:
Can Firestore update multiple documents matching a condition, using one query?
(8 answers)
Firestore update all documents in collections
(6 answers)
How to do a bulk update in Firestore
(4 answers)
Closed 9 months ago.
I have a function that allows a user to change their username, simply using firebase update({}). Every time a user sends a message in my chat page, it saves it as a documents on firebase with related data, user name, time message was sent etc.
The function below is for a chat page, which works how I want it to. it takes whatever the current users username and updates all past messages with the users new current username. I want to know if their is a better way to achieve this.
How can I change a single value in any amount of documents, which obviously have different ids, in one single call?
List<String> testList = []; //<--- List of document IDs of previous messages user has sent
FirebaseFirestore.instance
.collection('users')
.doc(loggedInUser.uid)
.update({'messageID': testList});
for (var item in testList) {
var collection = FirebaseFirestore.instance
.collection("chatrooms")
.doc(chatroom)
.collection("users")
.doc('ggg')
.collection("userMessage");
collection
.doc(item) // <-- Doc ID where data should be updated.
.update({'username': loggedInUser.userName.toString()}) // <-- Updated data
.then((_) => print('Updated'))
.catchError((error) => print('Update failed: $error'));
This question already has answers here:
Flutter & Firebase: Return list of collections
(2 answers)
Fetching all collections in Firestore
(3 answers)
How to list subcollection on firestore? [duplicate]
(1 answer)
Closed 1 year ago.
i want "a9XvvHPEvhfDWTXe8HqPqWuP2gg1" this documentId collection list.
FirebaseFirestore.instance
.collection('tbl_FriendList').doc(
'a9XvvHPEvhfDWTXe8HqPqWuP2gg1').get().then((event) {});
You can't do that with the Flutter sdk. You'll need to restructure your data.
Retrieving a list of collections is not possible with the mobile/web
client libraries. You should only look up collection names as part of
administrative tasks in trusted server environments. If you find that
you need this capability in the mobile/web client libraries, consider
restructuring your data so that subcollection names are predictable.
https://firebase.google.com/docs/firestore/query-data/get-data#list_subcollections_of_a_document
As per you image, seems like there is no document in this ID. You only have sub-collections.
To get sub-collection you have to improve your query and you should know your sub-collection name as mentioned below
await FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionPath')
.get();
Or you have to store data in db like this
a9XvvHPEvhfDWTXe8HqPqWuP2gg1 > collectionName > documents
FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionName')
.add({'data': 'data123'});
after that you can get data as mentioned below
FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionName')
.get()
.then((value) => {});
This query will return all data you have in collectionName sub-collection.
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.
I have an array or list of name "A", in which there are document Ids. Now I want to bring documents from cloud firestore, in the order in which their document Ids are stored in the list "A".
"whereIn:" Only fetches documents in the order they are in collection of cloud firestore, but i want to fetch in order as mentioned in the list.
Also, I am using ".getDocuments()", in order to fetch data,So please mention, if there any other efficient way of fetching data,which supports offline,caching.As, there can be some 200 to 400 documents.
Code:
List A;//Not empty :), contains ordered document ids of the collection
QuerySnapshot snapshot = await someCollectionRef.where('/*somefieldname*/', whereIn: A).getDocuments();
List<Users> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();//Here converting that snapshot data into list
please mention, if there is any other efficient way of fetching data as I am here using ".getDocuments()",which supports offline,caching.As, there may be some 200 to 400 documents.
Thanks for the help in advance :)
this should do what you want, just change the element you want to compare in the data document:
querySnapshot.documents.sort((a, b) {
return a.data['id'].compareTo(b.data['id']);
});
in the code above, i'm comparing the ID of these docs.