FireBase Firestore通过ID列表检索多个文档

时间:2018-07-06 14:26:25

标签: android firebase kotlin google-cloud-firestore

我有两个集合,例如CitiesHouses。房屋不是城市的嵌套集合,而是房屋包含对城市的引用。

Wat是获取列表中每个房屋的城市信息的最有效方法?

例如

class City {
  var name: String = ""
  var property1: String = ""
  var property2: String = ""
}

class House {
  var cityId: String = ""
  var propertyA: String = ""
  var propertyB: String = ""
}

我有一个房屋清单,如何为清单中的每座房屋获得城市名称?通过查询是否可行?或者我应该只获取所有城市(或每个城市分开)并在代码中映射它们。

城市和房屋只是一个例子,但与我的真实情况相似。

5 个答案:

答案 0 :(得分:2)

如果您要通过集合的文档ID来查询集合中的多个文档,则需要分别请求每个文档。当前尚无法制定单个查询来一次返回所有文档的方法。

答案 1 :(得分:2)

您可以使用com.google.firebase.firestore.FieldPath。这在Kotlin中为我工作:

//friendsIDs is ArrayList<String>
    firestore!!.collection("users").whereIn(FieldPath.documentId(), friendsIDs).get().addOnCompleteListener {usersDocs ->
        if (usersDocs.isSuccessful) {
            val result = usersDocs.result!!
            val docs = result.documents
            for (userDoc in docs) {
                val friendData = userDoc.data!!
                //...
            }
        } else {
            Log.e("FirestoreRequest", "Error getting documents.", usersDocs.exception)
        }
    }

答案 2 :(得分:1)

我能想到的最简单的方法是为每个名为cityName的房屋对象创建一个新属性。因此您的数据库结构应如下所示:

Firestore-root
   |
   --- houses (collection)
        |
        --- houseId (document)
              |
              --- propertyA: "Property A" (document propery)
              |
              --- propertyB: "Property B" (document propery)
              |
              --- cityName: "City Name"
  

如何为该列表中的每座房子获取城市名称?

只需查询您的houses集合并获取相应的城市名称即可。在代码中,应如下所示:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference ref = rootRef.collection("Posts").document("Post");
ref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String cityName = document.getString("cityName");
                Log.d("TAG", cityName);
            } else {
                Log.d("TAG", "No such document");
            }
        } else {
            Log.d("TAG", "get failed with ", task.getException());
        }
    }
});

因此,无需保留城市名称即可显示城市名称。

答案 3 :(得分:0)

这是通过Android中的ID列表获取多个文档的方法。

//Get Firestore instance
FirebaseFirestore ref = FirebaseFirestore.getInstance();

List<String> ids;

//... Fill ids list with document ids

//Fire query
ref.collection("collection_name").whereIn(FieldPath.documentId(), ids).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

答案 4 :(得分:0)

对于网络应用程序(JS),它可以: db.collection('myCollection').where(firebase.firestore.FieldPath.documentId(), "in", collectionIds).where('status', '==', 2),其中collectionIds是文档ID的数组。