我的firestore文档中有两个字段:一个是数字,另一个是注释,这是一个对象数组。
现在,我想向现有文档的comments数组中添加一个新注释。我知道我需要使用现有的注释创建一个临时列表,然后在该列表的末尾添加一个新的注释,最后使用Set方法将该临时列表放入firestore。我的问题是--如何做到这一点?我尝试使用下面的代码,但它只创建了一个对象列表,但我只想要一个对象列表。
FirebaseFirestore.getInstance()
.collection("name of my collection")
.document("name of particular document")
.get().addOnSuccessListener {
docs: DocumentSnapshot ->
//this creates list of list of objects
val data = listOf(docs.data!!["comments"])
val tempOutput = data.toMutableList()
tempOutput.add(newCommentInfo)
//This prints list of objects
Log.i(TAG, "${docs.data!!["comments"]}")
//This prints list of list of objects which I don't want
Log.i(TAG, "$tempOutput")
FirebaseFirestore.getInstance()
.collection("name of my collection")
.document("name of particular document")
.set(newCommentInfo, SetOptions.merge())
发布于 2019-05-24 03:26:33
如果您只是尝试更新文档中的单个列表类型字段(从您的问题中并不完全清楚),使用update()方法并告诉它您正在修改哪个字段会更容易:
FirebaseFirestore.getInstance()
.collection("name of my collection")
.document("name of particular document")
.update("comments", newCommentInfo)
https://stackoverflow.com/questions/56281656
复制相似问题