Firebase Firestore, get the reference document inside another document

前端 未结 2 1869
旧巷少年郎
旧巷少年郎 2020-12-11 17:47

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 r

相关标签:
2条回答
  • 2020-12-11 18:15

    You can get multiple documents from a collection. Read get mutiple documents section from documentation

    Here's an example

    db.collection("classes")
        .document(idOfTheClassYouWantToGet)
        .collection("users")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>(){
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        User user = document.toObject(Users.class);
                   }
                } 
            }
        });
    
    0 讨论(0)
  • 2020-12-11 18:16

    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?

    0 讨论(0)
提交回复
热议问题