简体   繁体   English

如何在 Firestore 中更改文档数组字段的某个索引的值?

[英]How to change the value of a certain index of an array field of a document in Firestore?

I have an angular app with cloud firestore with the following structure:我有一个带有云 firestore 的角度应用程序,其结构如下:

--nombre
--grupo
--nombre1
--nombre2
--nombre3
--numerocuenta1
--numerocuenta2
--numerocuenta3
--resuelto
  --0: false
  --1: false
  .
  .
  .
  --13: false

As you can see, at the end I have an array of boolean (resuelto) which contains 13 elements, I want to change a value of a certain index when an element with the same index is clicked如您所见,最后我有一个包含 13 个元素的布尔值 (resuelto) 数组,我想在单击具有相同索引的元素时更改某个索引的值

¿Is there a way to access each index of that array and change its value? ¿有没有办法访问该数组的每个索引并更改其值?

I tried with this:我试过这个:

this.taskDoc = this.afs.doc(`tasks/${task`);
this.taskDoc.update(resuelto[0]); <-- 

And this:还有这个:

this.taskDoc.update(resuelto.0); <--

Firestore doesn't support update of individual array elements. Firestore 不支持更新单个数组元素。 If you have an array and want to change one of its value, you'll have to read the entire array, change the value locally, then write the entire array back.如果您有一个数组并想更改其中一个值,则必须读取整个数组,在本地更改值,然后将整个数组写回。

It might be better for you to model your data as an object rather than an array.将数据建模为对象而不是数组可能更好。 Object properties may be individually updated by calling out the object field with the dot notation .可以通过使用点符号调出对象字段来单独更新对象属性。

While you cannot do operations on certain elements in an array by index, you can add or remove elements in an array by value using arrayUnion and arrayRemove:虽然您不能按索引对数组中的某些元素进行操作,但您可以使用 arrayUnion 和 arrayRemove 按值添加或删除数组中的元素:

doc.update("someField" : FieldValue.arrayUnion("someValue"))
doc.update("someField" : FieldValue.arrayRemove("someValue"))

This makes arrays act more like a set then an array but it at least gives you some options.这使得数组更像是一个集合而不是一个数组,但它至少给了你一些选择。 more documentation here: https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html更多文档在这里: https ://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Go 将文档 ID 作为字段值 Firestore 插入 - How to insert document id as a field value Firestore with Go Flutter 对文档数组字段的firestore查询 - Flutter firestore query on document array field 如何检查firestore数据库中的文档中是否存在字段? - how to check if Field exist in document in firestore database? Kotlin + Firestore:如何为firestore数据库的某些数据添加字段? - Kotlin + Firestore: How to add field to certain data of firestore database? 更新索引值时,Firestore addSnapshotListener 无法收听文档更新 - Firestore addSnapshotListener could not listen to update on document when updating the index value 如何获取在 flutter 中具有特定字段值的 Firestore 文档的 DocumentID? - How do I get the DocumentID of a firestore document that has a particular field value in flutter? Firestore 通过数组的字段值进行查询 - Firestore to query by an array's field value 如何将 append 数组放入 firestore 中的数组字段? - How to append an array into an array field in firestore? Firestore:如何在强制执行唯一字段值的同时插入具有自动 ID 的文档? - Firestore: How to insert a document with automatic id while enforcing unique field value? 如何从 Firestore 获取对文档数组的引用 - How to Get Reference to Document Array from Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM