Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Documents that has type reference in Firestore

According to Cloud Firestore=>Data type Documentation, type reference act as foreign key in NoSQL world. but when I query it in Fluter/Dart. Database in Firestore.

[Collection]

Note: DocumentOne's data type are all reference

  1. CollectionWithReference
    • DocumentOne
      • FKOne/doc001
      • FKTwo/doc002
  2. FKOne
    • doc001
      • "someData": 'Just Some Data'
  3. FKTwo
    • doc002
      • "anotherData": 'Just Another Data'

Example Code:

Firestore.instance.collection('CollectionWithReference').snapshots()
  .listen((data) => data.documents.forEach((document) => print(document.data)));

Output

{FKOne: Instance of 'DocumentReference', FKTwo: [Instance of 'DocumentReference', Instance of 'DocumentReference', Instance of 'DocumentReference']}

like image 762
Clueless Avatar asked Mar 26 '26 05:03

Clueless


1 Answers

The output in your question is to be expected.
Because your references are parsed as objects, print will only print out Instance of 'DocumentReference'.

Here you can take a look at the class DocumentReference, which contains all the necessary data about your reference.
In the following code I will print out the path (which is a getter of every DocumentReference object) of each of your references:

Firestore.instance.collection('CollectionWithReference').snapshots().listen((data) {
  data.documents.forEach((document) {
      print(document.data['FKOne'].path);
      document.data['FKTwo'].forEach((documentReference) => print(documentReference.path));
  });
});
like image 55
creativecreatorormaybenot Avatar answered Mar 27 '26 22:03

creativecreatorormaybenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!