[英]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.