简体   繁体   English

修改数组中单个元素的值 - firestore

[英]Modify the value of a single element in an array - firestore

I have the following set-up for a data structure:我对数据结构进行了以下设置:

users
|-name
|-id

businesses
|-name
|-id
|-owners
||[0]
||-name
||-id
||[1]
||-name
||-id

reviews
|-id
|-description
|-authorId
|-authorName

Based on the videos and blog posts, I know that I can remove elements and add elements using the new array functionality from 2018. Is there a way to pass a specific reference to the update method to update a specific value, like owner[1].name?根据视频和博客文章,我知道我可以使用 2018 年的新数组功能删除元素和添加元素。有没有办法将特定引用传递给 update 方法以更新特定值,例如 owner[1] 。姓名?

Watched the video on Multipath updates, read the blog post on arrays, searched around for examples of doing multipath updates, attempted to read about different methods of updating arrays on StackOverflow.观看有关多路径更新的视频,阅读 arrays 上的博客文章,四处搜索进行多路径更新的示例,尝试在 StackOverflow 上阅读有关更新 arrays 的不同方法。

    db.collection("businesses").whereArrayContains("owners",userId).get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        DocumentReference ref =db.collection("businesses").document(documentSnapshot.getId());
                        batch.update(ref, "Name", editText.getText().toString().trim());
                    }

No, the native array operations only operate on entire items within a list type field. 不可以,本机数组操作只能对列表类型字段中的整个项目进行操作。 You can't call out a specific property of an object in an array. 您不能在数组中调出对象的特定属性。 If you want to modify a property of an object in a list, you have to read the document, modify the list in your code, then update the entire field back to the document. 如果要修改列表中对象的属性,则必须阅读文档,在代码中修改列表,然后将整个字段更新回文档。 You will not be able to do this with a single operation. 您将无法通过单个操作执行此操作。

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

相关问题 如何在 firestore 数据库中向数组添加元素? (Python) - How to add element to array at firestore database? (python) 如何使用 Laravel 删除 Firestore 上的数组元素 - How to delete element of an array on Firestore using Laravel Firestore 更新数组字段中的单个项目 - Firestore Update single item in an array field 从 swiftui 中的云 firestore 数组中删除元素 - delete element from cloud firestore array in swiftui Firestore,将值添加到现有数组字段 - Firestore, adding value to an existing array field Firestore 规则:检查数组中是否存在某个值 - Firestore rules : check the existence of a value in array 尝试在 swift 应用程序的 Firestore 中添加数组元素时出现“无法将 'String' 类型的值转换为预期的参数类型 '[Any]'”错误 - "Cannot convert value of type 'String' to expected argument type '[Any]'" error when trying to add array element in firestore in swift app 如何获取特定的数组值 Cloud Firestore - How to get specific array value Cloud Firestore Firestore 通过数组的字段值进行查询 - Firestore to query by an array's field value 强制 Firestore 数组与已经是数组一部分的值合并 - Force the Firestore array to merge with the value that's already a part of the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM