[英]Cloud firestore: Update field values in nested array object with dot notation
请帮我解决这个问题,我想使用点符号更新字段,使用 set() 但每次我使用以下实现运行时。 我将字段添加到 firestore 作为例如studentInfo.0.course.0.courseId
而不是更新已经存在的字段。
位于 Firestore 中的 Json 示例
"schoolId": "school123",
"studentInfo": [
{
"studentId": "studentI23",
"regDate": "2020-04-18",
"course": [
{
"courseId": "cs123",
"regDate": "2020-05-28",
"status": "COMPLETED"
}
]
}
],
"registered":"yes"
}
代码逻辑
const query = firestore.collection('users').where('registered', '==', 'yes')
const students = await query.get()
students.forEach(student => {
firestore.doc(student.ref.path).set({
'studentInfo.0.studentId': '345','studentInfo.0.course.0.courseId': '555'
}, { merge: true })
})
在文档https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects我只能找到更新嵌套对象,但不能找到嵌套数组对象。
确实不可能使用点表示法或其他方式更新数组中的单个元素。 要更新数组,您需要:
唯一的替代数组操作是array-union
和array-remove
,它们向/从数组中添加和删除唯一元素 - 本质上将其视为一个数学集。 但是由于您要更新现有元素,因此这些操作在这里没有用。
另见:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.