【问题标题】:Firebase - Needing to find document id using 'where'Firebase - 需要使用“where”查找文档 ID
【发布时间】:2020-06-29 00:19:13
【问题描述】:

我需要找到我使用 .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 })
}

【问题讨论】:

标签: javascript database firebase google-cloud-firestore


【解决方案1】:

您似乎正在尝试更新查询产生的所有文档。 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(...)
    })
})

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多