使用.get()
,取得Firestore的內容
利用文件的ID查詢單一文件
Firestore.collection("Student")
.document("001")
.get()
.addOnSuccessListener { documentSnapshot: DocumentSnapshot ->
val student = documentSnapshot.toObject(Student::class.java)
}
取得整個集合的內容,利用迴圈將文件一筆一筆加入List中
var list = List<Student>
Firestore.collection("Students")
.get()
.addOnSuccessListener { documentSnapshot: DocumentSnapshot ->
val student: List<Student> = querySnapshot?.toObjects(Student::class.java) ?: mutableListOf()
for (i: Student in student) {
list.add(i)
}
}
whereEqualTo()
欄位值等於whereGreaterThan()
欄位值大於whereLessThan()
欄位值小於whereGreaterThanOrEqualTo()
欄位值大於或等於whereLessThanOrEqualTo()
欄位值小於或等於//尋找座號為1的學生
Firestore.collection("Students")
.whereEqualTo("number", 1)
.get()
如果連續使用的話則會是and邏輯搜尋,分開使用的話的則是or邏輯
//尋找座號為1且年齡大於10的學生
Firestore.collection("Students")
.whereEqualTo("number", 1)
.whereGreaterThan("age", 10)
.get()
//尋找座號為1或年齡大於10的學生
Firestore.collection("Students")
.whereEqualTo("number", 1)
Firestore.whereGreaterThan("age", 10)
.get()