Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore, get the reference document inside another document

I'm using Firebase Cloud Firestore, I want when I'm getting a document with reference field inside to receive the reference field as a document and not as a reference to the document...

I have a collection of users and collection of classes (at school) and each class contains a list of references to users (the students in this class). When I using document.get() (to get the class by its reference) it returns an object that contains a list of DocumentReference (of the users) so I need to call document.get() around 20 more times just to get the users as objects and not as a reference to them.

I want to be able to call document.get() once and it will bring me all the references as documents, like that: database and code

Is there an elegant way to do this?

like image 493
Roi Amiel Avatar asked Jan 06 '18 15:01

Roi Amiel


People also ask

How do references work in firestore?

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist. A DocumentReference can also be used to create a CollectionReference to a subcollection.

How do I find my collection reference on firestore?

CollectionReference collectionReference = FirebaseFirestore. getInstance(). collection("public_messages"); ArrayList<String> ids = new ArrayList<>(); //Contains your keys ArrayList<Task<QuerySnapshot>> tasks = new ArrayList<>(); for (String id : ids) { Task<QuerySnapshot> task = collectionReference.

Can two documents have the same ID in firestore?

New document with same ID should not be allowed in the same collection. It should be possible to fetch an already existing document from a previous import.

How do you get document reference in flutter firestore?

DocumentReference docRef = FirebaseFirestore. instance. doc("cards/WzU..."); and then use the get() method on this DocumentReference .


1 Answers

You can't instruct the Firestore client SDK to automatically follow document references during a fetch of a single document. You have to request each individual document. In other words, there is no "join" operation like you might expect in SQL.

See also: What is firestore Reference data type good for?

like image 139
Doug Stevenson Avatar answered Sep 27 '22 15:09

Doug Stevenson