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

Viewed 701

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

{
    "email": "test@gmail.com",
    "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?

2 Answers

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
      }
  });
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;
}
Related