Akhil Suseelan
Akhil Suseelan

Reputation: 217

How to query Firestore database with the reference type in springboot?

I want to get a list of users who have referred by another user. I have a user collection in firebase as follows:

{
    "email": "[email protected]",
    "isReferred": "true",
    "phone": "43532",
    "place": "India",
    "referredBy": "User/Lb53139TJpmlIO9lsQxD",
    "votedFor": "John Doe"
}

I want a service class that can fetch users referred by id Lb53139TJpmlIO9lsQxD. referredBy type is set to reference. Can someone show me an example or a reference on how to query the reference type?

Upvotes: 2

Views: 1470

Answers (2)

Rafael Lemos
Rafael Lemos

Reputation: 5829

Creating this answer as a community wiki since it's based on Doug Stevenson's comments.

You can provide a DocumentReference object to the query if you want to filter on a reference type field. There is no reference for this in the documentation but you can follow the example below for the document you shared in the question:

FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference referralUserRef = db.collection("User")
                                      .document("Lb53139TJpmlIO9lsQxD");
db.collection("User")
  .whereEqualTo("referredBy", referralUserRef)
  .get()
  .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> task) {
          //Do something
      }
  });

Upvotes: 1

Abhilav Srivastav
Abhilav Srivastav

Reputation: 1

import { AngularFireStore } from '@angular/fire/firestore';

constructor(public df: AngularFireStore) {}


//Get list of users
getAllAvailableUser(): Observable<any[]> {
    this.userList = this.df.collection('users').valueChanges();
    return this.userList;
}

Upvotes: 0

Related Questions