如何合并Firestore中两个集合的数据? [英] How to merge data from two collections in firestore?

查看:48
本文介绍了如何合并Firestore中两个集合的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firestore数据库中有两个集合1.项目2.用户

I have two collections in firestore db 1. projects 2.users

项目"具有一个字段"created_by",其中包含创建它的用户的ID.我想提取项目以及创建它的用户的所有字段.

'projects' has a field 'created_by' which hold the id of the user who created it. I want to fetch projects along with all fields of the user who created it.

以下代码可以工作,但是我不确定这是否是正确的方法,以及如何知道每个项目的用户数据提取何时完成.

Following code works but I am not sure whether it is the correct way of doing this and how do we know when the fetching of user data for every project is complete.

fun getProjects(){

    val works = arrayListOf<Work>()
    db.collection("projects")
        .get()
        .addOnSuccessListener { documents ->
            for (document in documents) {

                val work = Work(document.id,
                    document.data.get("title").toString(),
                    document.data.get("skills") as ArrayList<String>,
                    document.data.get("date_created") as Date)

                works.add(work)

                db.collection("users").document(document.data.get("created_by").toString()).get()
                    .addOnSuccessListener {
                        documentSnapshot ->
                            work.created_by = User(documentSnapshot.data?.get("name").toString(),
                                    documentSnapshot.data?.get("mob").toString(),
                                    documentSnapshot.data?.get("role").toString(),
                                    documentSnapshot.data?.get("rating").toString(),
                                    documentSnapshot.data?.get("skills") as ArrayList<String>
                                    )
                        Log.d("works", works.toString())

                    }
            }
        }
        .addOnFailureListener { exception ->
            Log.w(TAG, "Error getting documents: ", exception)
        }
}

推荐答案

如果该代码适用于您的用例,那么它可能是适合您的正确方法.由于Firestore不支持服务器端联接,因此执行客户端联接是一种常见方法.

If the code works for your use-cases, it is probably the right approach for you. Since Firestore doesn't support server-side joins, doing this client-side join is a common approach.

一些注意事项:

  1. 您正在使用 get 来获取项目和用户,这意味着您只能获取一次数据.没问题,但是我总是建议您仔细检查是否不应该收听实时更新.没有什么更好的硬性规定,所以只需考虑一下对用户和代码库的优缺点.

  1. You're using a get to get both the projects and the users, which means you get the data only once. There is no problem with that, but I always recommend double-checking if you shouldn't listen for real time updates. There's no hard rule on when this is better, so just consider what the advantages and disadvantages for your users and codebase may be.

请考虑您真正需要多少用户数据,以及在每个项目下复制所需的用户数据是否值得.如果复制数据,则无需执行嵌套的读取/客户端联接.实质上,您要存储更多的数据,并使写操作复杂化,以实现更可扩展的读操作,并在那里获得更简单的代码.

Consider how much user data you really need, and whether it's worth it to duplicate this user data that you need under each project. If you duplicate the data, you won't need to perform the nested reads/client-side join. You're essentially storing more data, and complicating your write operations, to get a more scaleable read operation, and simpler code there.

当您刚接触NoSQL时,通常很难掌握新的数据模型.在这种情况下,我建议您退房:

When you new to NoSQL it is often hard to get to grips with the new data model. In that case I recommend checking out:

  • NoSQL data modeling.
  • Getting to know Cloud Firestore.
  • Firebase for SQL developers (even though it's for the Firebase Realtime Database, it's still a great resource for learning about NoSQL in general).

这篇关于如何合并Firestore中两个集合的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆