我需要在Firestore中更新我的地图的某个数组条目 [英] I need to update a certain array entry of my Map in Firestore
问题描述
我要更新我的产品系列的产品.
I want to update a product of my products array.
我的收藏集中有一系列产品
I have an array of products in a collection
products[{
- 0
productPrice: 123
- 1
productPrice: 432
}]
在我的代码中,我已经传递了要在该系列产品中更新的元素的位置
In my code I have passed the position of the element I want to update in that array of products
suspend fun updateProductPrice(position:Int,shopId: String,price: Int): Resource<Unit> {
FirebaseFirestore.getInstance().collection("shops").document(shopId).update("products"[position]) //here I need to get productPrice and update its value to the price parameter in my method
return Resource.Success(Unit)
}
我需要更新那个数组中的productPrice(在位置0处)的值,但是我找不到方法
I need to update that productPrice (at the position 0) value whiting that array, but I dont find how
谢谢!
推荐答案
无法通过其索引更新数组.您将必须阅读文档,以所需的方式修改内存中的数组,然后将数组字段更新回文档.如果您需要原子操作,则可以使用事务.
It's not possible to update arrays by their index. You will have to read the document, modify the array in memory the way you want, then update the array field back to the document. You can use a transaction if you need the operation to be atomic.
这篇关于我需要在Firestore中更新我的地图的某个数组条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!