首页 > 解决方案 > 在 Firestore Kotlin 中创建文档链接?

问题描述

我在该用户中创建了一个应用程序,该用户可以为我在firestore中创建一个集合的页面添加书签

UserList(collection)
  -> UserIdgenerated(document)
     -> profile(sub-collection)
     -> fav(sub-collection)
        -> service_page (document)
           -> details

我得到了另一个(比如说第二个收藏)收藏

service(collection)
 -> service_page(document)
   -> details

现在我想为第二个集合文档添加书签并在片段中显示书签。 我的解决方案是将 service_page 详细信息复制到 userList fav 集合

问题有没有办法通过创建第二个收藏文档链接来创建收藏夹?

标签: androidkotlingoogle-cloud-firestore

解决方案


您可以将对该集合的引用直接存储到 Firestore 中,然后.get()在需要时调用以检索文档。在下面的示例中,我会将引用推送到数组中,您可以使用映射将引用包含在您可能拥有的任何其他数据中。

val userID = Firebase.auth.currentUser.uid
db.collection("UserList/$userID/fav")
  .document("service_page")
  .update("Bookmarks", FieldValue.arrayUnion(db.doc("service/service_page"))
  .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") }
  .addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) }

推荐阅读