Firestore -在kotlin中的对象数组中按项获取文档



如果我们有一个这样的集合:

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。

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

  1. 对象学生班:
val alex = Student(100, "Alex")
firestore.collection("Courses")
.whereArrayContains("students", alex))
.get()
.addOnCompleteListener{}
  1. 使用Map学生数据:
firestore.collection("Courses")
.whereArrayContains("students", mapOf("id" to 100, "name" to "Alex")))
.get()
.addOnCompleteListener{} 

[Math, Physics]