Javascript CloudFireStore:使用点表示法更新嵌套数组对象中的字段值

Javascript CloudFireStore:使用点表示法更新嵌套数组对象中的字段值,javascript,firebase,google-cloud-platform,google-cloud-firestore,Javascript,Firebase,Google Cloud Platform,Google Cloud Firestore,请帮助我解决这个问题,我想使用点表示法更新字段,使用set(),但每次运行时都使用以下实现。我将字段添加到firestore中,例如studentInfo.0.course.0.courseId,而不是更新现有的字段 位于firestore中的Json示例 "schoolId": "school123", "studentInfo": [ { "studentId&quo

请帮助我解决这个问题,我想使用点表示法更新字段,使用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 })
 }) 

在文档中,我只能找到更新嵌套对象,而不能找到嵌套数组对象。

使用点表示法或其他方法确实不可能更新数组中的单个元素。要更新阵列,您需要:

  • 阅读文件
  • 从中获取数组的当前值
  • 确定新的数组内容
  • 将整个更新的数组写回数据库
  • 唯一可供选择的数组操作是
    数组联合
    数组删除
    ,它们向数组中添加和删除唯一的元素-本质上将其视为一个数学集。但是,由于您希望更新现有的元素,因此这些操作在这里没有用处

    另见:


    非常感谢您的清晰回复和有用的相关链接@Frank我一直在想我缺少了什么。但显然,更新数组中的单个元素是一件非常麻烦的事情。