【问题标题】:How to get a document from a collection based on a particular field in that document如何根据该文档中的特定字段从集合中获取文档
【发布时间】:2018-01-14 07:27:30
【问题描述】:
const docRef = firestore.collection("orders").document("")

如果字段值为Admno:“11.11.1111”,我需要获取文档“147OiRKFYKA3WAo5d5mk”

这是我的数据库

感谢您的帮助。

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    official Firestore documentation中提到了这个案例:

    db.collection("orders").where("Admno", "==", "11.11.1111")
        .get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc.data()); //here's your doc
            });
        })
        .catch(function(error) {
            console.log("Error getting documents: ", error);
        });
    

    如果您刚开始使用 Firestore,我强烈建议您阅读 the documentation

    【讨论】:

    • 谢谢,但是这个工作const docRef = firestore.collection("orders").document(doc.id);,因为我收到一个错误Uncaught (in promise) TypeError: firestore.collection(...).document is not a function 在代码行docRef.add({
    • 我必须再次指出documentationdocument() 不是函数。你必须使用const docRef = firestore.collection("orders").doc(doc.id);
    • 谢谢,它解决了问题,但是 docRef.add 创建了另一个文档来输入新字段并且 docRef.set 用新字段替换旧字段的值,有什么办法可以保留两者同一字段中的值。
    • 是的,您只需将不同的 id 传递给您的 doc() 函数
    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2023-01-31
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多