如何在 firestore android 中对其他集合详细信息进行分页

How can I paginate with other collection details in firestore android

我可以轻松地使用 Firestore 集合对 Recyclerview 进行分页。但就我而言,我有三个依赖集合。他们是

  1. 产品(预计 10000000 个条目)
  2. 类别(预计 100000 个条目)
  3. 区域(预计 50000 个条目)

在 Recyclerview 中,我必须显示产品、类别和区域的详细信息。正如我们所知,没有像 SQL 这样的内连接查询选项。我在产品详细信息中存储了类别 ID 和区域 ID。根据 id,我需要对产品进行筛选和排序。同样根据地区,我需要更改产品列表中的货币。如何使用 Firestore 实现这些复杂的架构以满足正常的 SQL 功能?就我而言,Firestore 已确认。

产品

{
    "productId": 122,
    "productName": "product name 1",
    //other product details here
    "categoryId": 758,
    "regionId": 395
}

类别

{
    "categoryId": 90474,
    "categoryName": "category name 200",
    //other category configuration details here
}

地区

{
    "regionId": 2372,
    "regionName": "tokyo",
    //other region details here
}

要根据类别和区域进行查询,您应该首先读取它们的ID。所以假设用户选择“类别名称200”和地区“东京”,为了得到相应的产品,请使用以下代码行:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference categoryRef = rootRef.collection("Category");
CollectionReference regionRef = rootRef.collection("Region");
CollectionReference productRef = rootRef.collection("Product");

categoryRef.whereEqualTo("categoryName", "category name 200").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                String categoryId = document.getString("categoryId");
                regionRef.whereEqualTo("regionName", "tokyo").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                String regionId = document.getString("regionId");

                                //Perform a query to get the products
                                productRef.whereEqualTo("categoryId", categoryId).whereEqualTo("regionId", regionId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                        if (task.isSuccessful()) {
                                            for (QueryDocumentSnapshot document : task.getResult()) {
                                                String productName = document.getString("productName");
                                                Log.d(TAG, productName);
                                            }
                                        } else {
                                            Log.d(TAG, "Error getting documents: ", task.getException());
                                        }
                                    }
                                });
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

一定要有唯一的类别和地区,这样每个查询returns一个结果。

如果您需要对结果进行排序,您还可以链接一个 orderBy() 调用,根据特定 属性 对产品进行排序。另请注意,此类查询需要

另请参阅下面对 Firestore 中的结果进行分页的简单解决方案: