![](/img/trans.png)
[英]How to update an "array of objects" with Firestore and Firebase functions?
[英]How to update data in an array of objects in Firestore using Kotlin?
如果要修改数组类型字段的单个对象,则必须读取文档,在客户端修改数组,然后将修改后的数组更新回 Firestore 中的文档。
请注意,如果不先阅读文档,您就无法做到这一点。 假设您有一个如下所示的数据类:
data class Place(
var id: String? = null,
var isCancelled: Boolean? = null,
var price: String? = null,
var reservationName: String? = null,
var reservationPhone: String? = null,
)
为了读取对象数组,我能想到的最简单的方法是将数组作为“Place”对象List<Place>
( List<Place>
),然后根据您的需要更新所需的元素。
回答可能会迟到,但这是解决方案
val washingtonRef = db.collection("cities").document("DC")
// Atomically add a new region to the "regions" array field.
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"))
// Atomically remove a region from the "regions" array field.
washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"))
这是您还可以阅读的链接
https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.