有没有一种方法可以批量读取Firebase文档 [英] Is there a way to batch read firebase documents

查看:64
本文介绍了有没有一种方法可以批量读取Firebase文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Firebase的flutter作为后端制作移动应用程序.

I am making a mobile app using flutter with firebase as my backend.

我有一个收集用户信息的用户文档集合.字段之一是引用数组(另一个集合中的引用文档),我想在批处理之类的操作中使用该引用数组,这样便可以读取所有文档.

I have a collection of user document that stores user information. one of the fields is an array of references (reference documents in another collection) which I want to use in an operation like batch that in that would then allow be to read all the documents.

我知道批处理只允许对数据库进行写操作,我的第二个选择是事务",它要求在读取之后进行写操作,这是我想避免的事情.

I know batch only allows writes to the database, My second option would be Transaction, which requires writes after reads which I am trying to avoid.

是否可以通过一种操作读取多个文档而无需使用事务处理?

Is there a way to read multiple documents in one operation without having to use Transaction?

推荐答案

Firestore不提供正式的批量读取API.正如弗兰克在评论中提到的那样,有一种方法可以使用 IN 使用其ID从单个集合中获取多个文档.但是,所有文档都必须位于同一集合中,每个查询最多不能超过10个文档.您最好只对每个文档分别进行get(),因为 IN 查询有局限性,并且不能保证执行得比单独获取要快.两种解决方案都不能保证是一致的",因此在任何给定的时间点,所取回的任何一个文档都可能比其他任何一种都更新鲜".

Firestore doesn't offer a formal batch read API. As Frank mentions in his comment, there is a way to use IN to fetch multiple documents from a single collection using their IDs. However, all of the documents must be in the same collection, and you can't exceed 10 documents per query. You might as well just get() for each document individually, as the IN query has limitations, and isn't guaranteed to execute any faster than the individual gets. Neither solution is guaranteed to be "consistent", so any one of the documents fetched could be "more fresh" than the others at any given moment in time.

这篇关于有没有一种方法可以批量读取Firebase文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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