简体   繁体   English

如何检索集合中的所有文档并查看 firebase Firestore 中是否存在文档?

[英]How do to retrieve all documents in a collection and see if a document exists in firebase Firestore?

Schema: This is how my schema looks架构:这就是我的架构的外观

Current Implementation:当前实施:

    for (let i=0; i<data.length; i++) {
            try
            {
                
                var ifPresent = db.collection("Safes-Hardware").doc(data[i]['Mac address Check']);
                ifPresent.get()
                .then(async (doc)=>{

                            if (!doc.exists) 
                            {
                                // Do stuff
                            }
                            else
                            {
                                //Do stuff
                            }
                            return { message: "Success is within the palm of our hands." } 
                        }

            }

Problem: Even though this code does the job, for each data in the array I'm doing a lookup, and this results in a socket hang-up.(sometimes) So I'm thinking I'll get all the documents in the collection in one go, store it locally and look up if a documents exists locally instead of querying the database every time.问题:尽管这段代码完成了这项工作,但对于数组中的每个数据,我都在进行查找,这会导致套接字挂起。(有时)所以我想我会在集合在一个 go 中,将其存储在本地并查找本地是否存在文档,而不是每次都查询数据库。

Question: How do I implement this?问题:我如何实现这个?

You can just use collection("Safes-Hardware").get().then() and you can save the data locally.您可以只使用collection("Safes-Hardware").get().then()并且可以将数据保存在本地。

let collection = []
db.collection("Safes-Hardware").get().then(function(querySnapshot) {
    collection = querySnapshot.map((doc) => ({
            id: doc.id,
            ...doc.data()
          }))
});

then you can use collection to search for what you want, maybe like this然后你可以使用collection来搜索你想要的,也许是这样的

data.forEach( doc => {
   let x = collection.find(v => v.id === doc['Mac address Check'])
   if(x){
    //it exists
   }else{
     // not exists
   }
})

But take care you are compromising bandwidth or number of requests with o(n^2) operation in the client side但请注意,您在客户端使用 o(n^2) 操作会损害带宽或请求数量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Firebase函数从文档中查询数字并将其总和成Firestore中的父文档 - How do a query a number from documents and sum it all up into a parent document in firestore using firebase functions 如何从集合中检索所有文档及其相关信息? (反应原生 Firebase) - How do i retrieve all documents from a collection and its relevant information? (React Native Firebase) 如何在 firebase firestore 中的集合文档之间循环? - How to loop between documents of a collection in firebase firestore? 如何检查集合中是否存在文档 (Firebase REACT)? - How do I check if a document exists within a collection (Firebase REACT)? 如何获取 Firebase Cloud Firestore 中存在/不存在特定字段的文档? - How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore? Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents 如何在 Firestore 中“获取集合中的所有文档”? - How to "Get all documents in a collection" in Firestore? 如何删除firestore集合数据库中的所有文档 - How to delete all the documents in a firestore collection database 如何获取 firestore 集合中的所有文档? - How to get all documents in a collection in firestore? 我可以获取所有文档,但无法在 Firestore 中检索单个文档 - I can get all documents but unable to retrieve single document in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM