减少在哪里查询中的读取量
我最近将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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您误解了文档和字段的定义。当您阅读文档时,您始终将所有字段从中获取。快照包含读取的所有内容,即使您不使用它。除了将其全部保存所需的存储空间外,每个场没有额外的费用。在您的屏幕截图中,您显示了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.