Firebase-需要使用“ where”查找文档ID

时间:2020-03-17 21:30:33

标签: javascript database firebase google-cloud-firestore

我需要找到通过.where(...)获取的文档的文档标签(名称/ id?)

我在做什么错了?

   // updates dev team in the db
function updateDevTeam(devToUpdate, update) { // devToUpdate = id of dev, fieldToUpdate = goal, name, or team_id, update = value
    var devToUpdateDocument = fb.db.collection('dev').where('id', '==', devToUpdate) //how do I set this to the document id(/name/path?)
    fb.db.collection('dev').doc(devToUpdateDocument).update({ team_id: update })
}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试更新查询产生的所有文档。 Firestore没有像SQL这样的“更新位置”功能。

如果您没有对该文档的引用,则无法更新该文档。要更新所有与查询匹配的文档,您必须:

  1. 实际执行查询
  2. 迭代结果中的所有文档
    • 获取文档参考
    • 单独更新
// Your query object
const query = fb.db.collection('dev').where('id', '==', devToUpdate)
// now actually perform the query to get a QuerySnapshot
query.get().then(qsnapshot => {
    // then iterate each document in the result set to update it
    qsnapshot.docs.forEach(dsnapshot => {
        // get a reference to the document in the DocumentSnapshot
        const ref = dsnapshot.ref
        // and update it
        ref.update(...)
    })
})