i tried this .Sorry im a beginner.i cant put a document I'd in the unique I'd place as there are multiple documents under which userProducts collection comes. moreover new documents will keep generating as users increase.Inside every new document generated there's a subcollection created called userProducts.Is any other way ?
Future getProducts() async {
var firestore = Firestore.instance;
QuerySnapshot snapshot = await firestore.collection('products'). documents (doc id).collection('userProducts').getDocuments();
// .orderBy('timestamp', descending: true)
return snapshot.documents;
Related
I want to add data and collection both I am trying like this
Future<void> createRoom(collection, docid, data) async {
await FirebaseFirestore.instance
.collection(collection)
.doc()
.set(data)
.collection('messages');
}
But its showing error The method 'collection' isn't defined for the type 'Future'.
If you are trying to set a document in a sub-collection then first create a DocumentReference to that collection and then use set():
await FirebaseFirestore.instance
.collection(collection)
.doc("doc_id")
.collection('messages')
.doc("msg_id")
.set(data)
Do note that you should pass document ID in doc() else it'll generate a random ID every time instead updating existing one.
I want to add data and collection both
You don't need to create a collection explicitly if you are not adding a document yet. It'll be created at the time you add first document in collection. So try refactoring the code as shown below:
Future<void> createRoom(collection, docid, data) async {
await FirebaseFirestore.instance
.collection(collection)
.doc("doc_Id")
.set(data);
// simply add a document in messages sub-collection when needed.
}
Also checkout: How to create collection in Cloud Firestore from Javascript?
I have following fields in a document at Firestore
Category: Online, OnSite,
Status: true, false,
postTime: mm.dd.yyyy.hh.mm.ss
I am using following method to fetch the snapshot from Firebase with filter on Status & postTime.
Future getData() async {
QuerySnapshot qn = await FirebaseFirestore.instance
.collection("items")
.where("status", isEqualTo: true)
.orderBy("postTime", descending: true)
.get();
return qn.docs;
}
Now that I have qn with a copy of Firestore data, can I run more specific queries OFFLINE on qn where I can further filter it with Category ?
Purpose is: If the app user wants to further filter/sort some data then I do not want to consume data from Firestore for every query since the main data is already fetched.
Dart has a convenient method called where for iterable objects that you can utilize to filter in various ways.
https://api.flutter.dev/flutter/dart-core/Iterable/where.html
Example:
final filteredDocs = qn.docs.where((doc) => doc.data()['status'] == 'pending').toList();
i have this database structure in Firestore:
this what inside places
how can i make a query in flutter to get all data inside Places sub-collection along with name from User collection, this code to get all places sub-collection data from every User collection :
Future<List<PlaceModel>> getPlaces() async {
List<PlaceModel> placesList = [];
// get all docs from user collection
var users = await udb.get();
for( var uid in users.docs) {
var userData = await udb.doc(uid.id).get();
var userPlaces = await udb.doc(uid.id).collection(placeTable).get();
userPlaces.docs.forEach((place) {
placesList.add(PlaceModel.fromMap(place.data()));
});
}
return placesList;
}
You can also use where() or orderby() methods to get data with some rules.
Firestore.instance
.collection("users")
.document(uid.id)
.collection(placeTable)
.where('fieldName', isEqualTo:name )
.snapshots()
A single query can only access a single collection, or a group of collection that have the same name. Firestore queries are shallow and don't read data from subcollections. If you need the data from the subcollection, you will have to execute an additional query to read it.
I would like to update a sub-collection document that I got by sending a group-query with Flutter. To my current understanding with a group-query I do not really know the parent of a sub-collection.
In order to do so, I need the document id of the parent document. The update query would then look like the following:
collection(collectionName)
.document(parentDocumentId)
.collection(subCollectionName)
.document(subCollectionDocumentId)
.updateData(someMapWithData);
Is it necessary to save the parentDocumentId within the sub-collection document to be able to do such update or is there another way to do so?
If you want to update a document inside a subcollection, then you need both the top document id and the document id inside the subcollection.
Is it necessary to save the parentDocumentId within the sub-collection document to be able to do such update or is there another way to do so?
No, its not necessary, but if you have the parentDocumentId, and you dont have the subDocumentId, then you need to query to retrieve it:
Firestore.instance
.collection("path")
.document("docPath")
.collection("subCollection")
.where("name", isEqualTo: "john")
.getDocuments()
.then((res) {
res.documents.forEach((result) {
Firestore.instance
.collection("path")
.document("docPath")
.collection("subCollection")
.document(result.documentID)
.updateData({"name": "martin"});
});
});
Unfortunately, I couldn't find an option to do this more cost-efficient. With this method, you have 1 read and 1 write operation.
Code for the following storage structure: users/${uid}/collection/$documentName/
static Future updateSubData(String uid, String mainCollection,
String subCollection, Map<String, dynamic> json) async {
//get document ID
final querySnapshot = await fireStoreInstance
.collection(mainCollection)
.doc(uid)
.collection(subCollection)
.limit(1)
.get();
//document at position 0
final documentId = querySnapshot.docs[0].id;
//update this document
await fireStoreInstance
.collection(mainCollection)
.doc(uid)
.collection(subCollection)
.doc(documentId)
.update(json);
}
I case you have more documents in the subcollection you need to remove .limit(1) so you get all documents.
I want to return all my documents in Firestore my document in Identifier based on the name but when I do my function its returns to me With the same number I have documents but the name its different instance of DocumentSnapshot but I need to return same names I have. How can I do this?
Future getallDocument()async{
final QuerySnapshot result = await Firestore.instance.collection("Institute").document(widget.id).collection("Ravs").getDocuments();
final List<DocumentSnapshot> documents = result.documents;
documents.forEach((data) {
print(data);
});
}