减少在哪里查询中的读取量

发布于 2025-02-06 03:16:01 字数 949 浏览 2 评论 0原文

我最近将firebase实施到我的项目中,并且创建了一个用户集合,每个用户都有一个文档,每个文档都有大约8个字段,当我的用户启动该应用程序时,我正在尝试拉动与他的相对应的文档数据,因此我要进行以下查询:

async function getUserData() {
        
        const _collection = collection(db, "users")
        const _query = query(_collection, where("userid", "==", uniqueUserID))
        const querySnapshot = await getDocs(_query)
        querySnapshot.forEach((doc) => {
            console.log(doc.data())
        })
        setLoadingStatus(false)
    }

此查询有效并为我提供相应的用户数据,但是问题是,如果用户在集合中太远,这将执行每个文档的8个读取,直到到达相应的用户,我试图使用LastModified实现缓存系统,但我仍然需要读取该字段的文档数据,并且最终将使用或多或少地使用相同数量的读取。我的问题是:我如何减少我在尝试比较文档中值时执行的读取操作的数量,我还考虑过添加a so a_unique> a_uniqueuserid >因此,它会按字母顺序排列,并占据文档的第一名,但它是骇人听闻的。 编辑:这是我的结构的样子: ”在此处输入图像描述”

I have recently implemented firebase into my project and I have created a user collection, this collection has a document for each user and each document has about 8 fields, when my user launches the app, I am trying to pull the document that corresponds to his data, so im doing the following query:

async function getUserData() {
        
        const _collection = collection(db, "users")
        const _query = query(_collection, where("userid", "==", uniqueUserID))
        const querySnapshot = await getDocs(_query)
        querySnapshot.forEach((doc) => {
            console.log(doc.data())
        })
        setLoadingStatus(false)
    }

This query works and gives me the corresponding user data, but the problem is, if the user is too far down the collection, this will execute 8 reads per document until it gets to the corresponding user, I have tried to implement a cache system using a lastModified but I still need to read the document data for that field and it will end up using more or less the same amount of reads. My question is: How do I reduce the amount of read operations that get executed when im trying to compare values in the documents, I have also thought of adding an a like so a_uniqueUserID so it gets ordered alphabetically and takes the first spot of the document but it's hacky.
EDIT: Here is what my structure looks like: enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2025-02-13 03:16:01

我认为您误解了文档和字段的定义。当您阅读文档时,您始终将所有字段从中获取。快照包含读取的所有内容,即使您不使用它。除了将其全部保存所需的存储空间外,每个场没有额外的费用。在您的屏幕截图中,您显示了5个文档,其中一个文档中有8个字段。

您可能正在误解控制台中的指标。当您使用控制台读取和编写文档时,这些文档也会按读取和写入 - 使用控制台不是“免费”。您所看到的是将应用程序与您在控制台中所做的工作结合在一起的组合。

I think you are misunderstanding the definition of a document and a field. When you read a document, you always get all fields out of it. The snapshot contains everything read, even if you don't use it. There is no additional cost per field, other than the storage required to hold it all. In your screenshot, you show 5 documents, and one of those documents have 8 fields.

You are probably misunderstanding the metrics in the console. When you read and write documents using the console, those are also billed as reads and writes - use of the console is not "free". What you are seeing is a combination of what your app is doing in combination with what you're doing in the console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文