我的firestore文档中有两个字段:多个注释是数字,而注释是对象数组。
现在,我想在现有文档中的注释数组中添加新注释。我知道我需要使用现有注释创建一个临时列表,然后在该列表的末尾添加一个新注释,最后,使用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())
答案 0 :(得分:0)
如果您只是尝试更新文档中的单个列表类型字段(问题尚不完全清楚),则使用update()方法并告诉您要修改的字段会更容易:< / p>
FirebaseFirestore.getInstance()
.collection("name of my collection")
.document("name of particular document")
.update("comments", newCommentInfo)