[英]Modify the value of a single element in an array - firestore
我对数据结构进行了以下设置:
users
|-name
|-id
businesses
|-name
|-id
|-owners
||[0]
||-name
||-id
||[1]
||-name
||-id
reviews
|-id
|-description
|-authorId
|-authorName
根据视频和博客文章,我知道我可以使用 2018 年的新数组功能删除元素和添加元素。有没有办法将特定引用传递给 update 方法以更新特定值,例如 owner[1] 。姓名?
观看有关多路径更新的视频,阅读 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());
}
不可以,本机数组操作只能对列表类型字段中的整个项目进行操作。 您不能在数组中调出对象的特定属性。 如果要修改列表中对象的属性,则必须阅读文档,在代码中修改列表,然后将整个字段更新回文档。 您将无法通过单个操作执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.