如何在Firestore中检查是否存在具有给定ID的文档,而无需花费金钱? [英] How to check if a document exists with a given id in firestore, without costing money?

查看:83
本文介绍了如何在Firestore中检查是否存在具有给定ID的文档,而无需花费金钱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中有用户的电话号码,我想检查用户是否已经在我的应用程序中注册.为此,我在Firestore中有一个收藏.在此集合中,我将单个用户的联系电话作为文档.每当用户打开该应用并输入其手机号码时,该应用都会发送请求,以使用

I have a scenario where I have the phone number of the user and I want to check if the user is already registered on my app or not. To do this, I have a collection in firestore. In this collection, I the contact number of the individual user as a document. Whenever the user goes on the app and enters his mobile number, the app sends the request to search a specific document using

final snapShot = await Firestore.instance.collection('rCust').document(_phoneNumberController.text).get();

我的数据库结构如下

由于这个原因,我的Firestore帐单增长很快.仅用4-5个查询,我的读取次数就从75个增加到293个.如果有人可以指导我如何有效地执行此操作,那将是非常不错的.

Due to this, my firestore billing is spiking up really fast. In just with 4-5 queries, my number of reads spiked from 75 to 293. It would be great if anyone could guide me in how to do this efficiently.

推荐答案

要检查文档是否存在,可以根据需要在documentSnapshot中使用.exists属性:

To check if a document exists, you can use the .exists propety in the documentSnapshot, in your case:

if(snapShot.exists) {

}

从该查询中,您选择的是单个文档,而不是集合.

From that query, you are selecting a single document, not a collection.

因为我们看不到其他代码,所以我假设您的Firestore使用量实际上不是由于查询而增加,而是由于您在Firebase Web控制台中查看了文档.在网络上查看控制台也会产生费用,并一次列出300个文档.

Because we can't see other code, I am assuming that your firestore usage is actually not spiking due to your query, but due to you viewing your documents in the firebase web console. Viewing the console on the web also incurrs billing, and lists documents 300 at a time.

这篇关于如何在Firestore中检查是否存在具有给定ID的文档,而无需花费金钱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆