Get document id when searching with where clause firestore

删除回忆录丶 提交于 2021-01-29 05:18:11

问题


I want to be able to get the document id of a document that I have just queried with a where clause in firestore. Here is an example that I have at the moment:

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

From this, I want to be able to get the document id of what I have just queried. Is there any way I can do that?

Thanks in advance.


回答1:


an example solution to what you're looking for can be found in the Firestore documentation:

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);
});

You can simply call doc.id to retrieve the document's ID.

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



来源:https://stackoverflow.com/questions/56864652/get-document-id-when-searching-with-where-clause-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!