【问题标题】:Cloud Firestore Data Structure making effectivelyCloud Firestore 数据结构有效制作
【发布时间】:2022-01-21 23:20:14
【问题描述】:

我正在考虑 RecyclerView。

例如,用户可以按地区查看帖子。所以我想根据 post1,post2,post3, ...

我不知道这样想是否可以。

【问题讨论】:

  • 所以你想按地区过滤帖子,就这样?
  • @AlexMamo 是的,是的!但我是初学者。所以我这样想是因为这看起来很容易。

标签: java android firebase google-cloud-platform google-cloud-firestore


【解决方案1】:

创建一个如下所示的集合是有意义的:

Firestore-root
  |
  --- posts (collection)
       |
       --- $postId
       |     |
       |     --- region: "USA"
       |     |
       |     --- title: "Post one title"
       |
       --- $postId
             |
             --- region: "Europe"
             |
             --- title: "Post teo title"

如果您想按特定区域过滤帖子,那么您只需执行如下所示的查询:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference postsRef = db.collection("posts");
Query queryByRegion = postsRef.whereEqualTo("region", "Europe");
queryByRegion.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document != null) {
                    String title = document.getString("title");
                    Log.d(TAG, title);
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage());
        }
    }
});

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 2023-03-26
    • 1970-01-01
    • 2021-06-19
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多