【问题标题】:Get document id when searching with where clause firestore使用 where 子句 firestore 搜索时获取文档 ID
【发布时间】:2019-07-03 15:15:43
【问题描述】:

我希望能够获取我刚刚使用 firestore 中的 where 子句查询的文档的文档 ID。这是我目前的一个例子:

db.collection("Users").where('fullname', '==', 'foo bar').limit(5).get()
     .then(function (doc)
    {
        querySnapshot.forEach(function (doc)
        {
          //find document id
        }
    }

由此,我希望能够获取我刚刚查询的文档 ID。有什么办法可以做到吗?

提前致谢。

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    您正在寻找的解决方案示例可以在 Firestore 文档中找到:

    db.collection("cities").where("capital", "==", true)
    .get()
    .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });
    

    您只需调用doc.id 即可检索文档的ID。

    来源:https://cloud.google.com/firestore/docs/query-data/get-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2021-07-22
      • 2018-10-15
      • 2018-10-31
      • 2018-04-21
      • 2018-10-20
      相关资源
      最近更新 更多