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

时间:2019-05-23 19:27:28

标签: java android arrays firebase google-cloud-firestore

我对数据结构进行了以下设置:

users
|-name
|-id

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

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

基于视频和博客文章,我知道我可以使用2018年以来的新数组功能删除元素并添加元素。有没有一种方法可以将特定引用传递给update方法以更新特定值,例如所有者[1] .name?

观看了有关多路径更新的视频,阅读了有关数组的博客文章,搜索了进行多路径更新的示例,并尝试了解有关在StackOverflow上更新数组的不同方法。

    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());
                    }

1 个答案:

答案 0 :(得分:0)

否,本机数组操作仅对列表类型字段中的整个项目起作用。您不能在数组中调出对象的特定属性。如果要修改列表中对象的属性,则必须阅读文档,在代码中修改列表,然后将整个字段更新回文档。您将无法通过单个操作来做到这一点。