【问题标题】:Firestore - get documents by item in array of object in kotlinFirestore - 在kotlin的对象数组中按项目获取文档
【发布时间】:2021-10-05 05:20:54
【问题描述】:

如果我们有这样的集合:

Courses:
 Math:
    id: 1001
    students: [
        {id: 100, name: "Alex"},
        {id: 101, name: "mark"}
    ]
 Physics:
    id: 1002
    students: [
        {id: 100, name: "Alex"},
        {id: 103, name: "arnold"}
    ]

我们如何获得特定学生的所有课程? 例如将所有课程分配给 Alex。

【问题讨论】:

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:

我们可以通过两种方式获得它:

  1. 学生类的对象
val alex = Student(100, "Alex")

firestore.collection("Courses")
            .whereArrayContains("students", alex))
            .get()
            .addOnCompleteListener{}
  1. 使用学生数据的地图
firestore.collection("Courses")
            .whereArrayContains("students", mapOf("id" to 100, "name" to "Alex")))
            .get()
            .addOnCompleteListener{} 

这将返回以下文档:
[Math, Physics]

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2018-05-24
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多