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.
Related
Apparently, I was able to retrieve a subcollection data from firebase by using the code below
FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
However, I am now trying to delete a subcollection by using this collectionGroup method and it is not working. I want all admin user to be able to delete the subcollections.
So what I found online is something similar to this line of code.
// define document location (Collection Name > Document Name > Collection Name >)
var docRef = Firebase.firestore().collection("Rooms").doc("bsYNIwEkjP237Ela6fUp").collection("Messages");
// delete the document
docRef.doc("lKjNIwEkjP537Ela6fhJ").delete();
which I got from codegrepper but as far as I know, the doc("bsYNIwEkjP237Ela6fUp") part requires the id of the user which is not the method I want because all admin users should be able to delete the data and it uses collection instead of collectiongroup
Is there any solution for this ? Please help.
FirebaseFirestore.instance.collection("Rooms").doc(snapshot.requireData.docs[i].id).collection("Messages").doc(snapshot.requireData.docs[i].id).delete();
I want check within a list of 10 document IDs how many exists in a collection. One way to do it would store the document ID inside the document and use in operator.
const result = await this.afs.collection(path).where('id', 'in', ['doc1', 'doc2']).get();
Is there a way we can avoid storing the document id inside document.
For this particular query, you can use FieldPath.documentId(). It returns a token that you can use in the field path of the query:
this.afs.collection(path).where(FieldPath.documentId(), 'in', ['doc1', 'doc2'])
I've linked you to the plain JavaScript documentation. If you're using Angular, it might have a slightly different way of getting this token value, but it will be in a class called FieldPath.
A Firestore collection activities contains a field called member. That field is a Document Reference to a document in the members collection.
Is there a way to filter all activities for that member if all I have is the member document id?
E.g. something like:
firebase.collection('activities').where('member.id', '==', memberId)
The closest I got was
firebase.collection('activities').where('member', '==', firebase.collection('members').document(memberId)
but that constitutes an extra read operation.
This code firebase.collection('members').document(memberId) sets up a DocumentReference. It does not yet read that document, so there's no charge for it.
Your last snippet looks correct to me: to find all activities with a reference to a specific member, you need to set up a DocumentReference to that member's document.
I want to get a single Observable from a collection, but I want to get it from a different field that is not the id. It is possible?
I do not want to do a query and limit to 1. I need to get a Single Observable not an array Observable.
Schema:
Code:
this.afs.doc<Credit>('credits/uid/'+ uid).valueChanges();
Error:
Invalid document reference. Document references must have an even number of segments, but credits/uid/d1Zt8sozYqb6H27lhoJgF1Gx2Cc2 has 3
I am not sure if I understand correctly, but I guess that you want to get document with particular uid field value, not using document id.
This particular error is related with common feature of Firestore, that every document has to be in collection not in document. So path values for documents, (nested as well) are always checked, if the segments (devided by /) number is even ex. collection1/doc1/collection2/doc2/collection3/doc3
As results in your code we have 3 segments (like credits/uid/<uid_value>) so this is the error.
I am not very familiar with angularFire2 itself, but I have done it in JS. The approach is normally to query collection and than use method on the results, which in classic JS returns Query object on which the same methods can be used as on CollectionReference (which extends 'Query' btw - reference 1).
Combining this approach with those references: querying and collection I propose following solution:
this.afs.collection('credits', ref => ref.where('uid', '==', <uid_value>)).valueChanges()
If uid_value will be unique you should get your doc.
Unfortunately I do not have any playground to test the solution so please let me know how it works - or if there will be any additional errors.
I hope it will help! Good Luck!
I have a collections called 'users' and I want to add new field called 'status: boolean' to already exists every documents using cloud functions and firestore database instantly.
There is no bulk update operation, so you will have to:
Query the collection for all documents
Iterate each one
Update the document with the new field