[英]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.