【问题标题】:How to retrieve all documents from a collection in Firestore optimizing reads?如何从 Firestore 优化读取的集合中检索所有文档?
【发布时间】:2022-09-27 21:19:27
【问题描述】:

基本上我对使用以下代码获得的读取量有疑问:

 public Iterable<Contract> findAllExpired(){
    List<Contract> empList = new ArrayList<Contract>();
    CollectionReference collaborator = fb.getFirestore().collection(\"Contracts\");
    ApiFuture<QuerySnapshot> querySnapshot = collaborator.get();
    try {
        for (DocumentSnapshot doc : querySnapshot.get().getDocuments()) {   
            Contract emp = doc.toObject(Contract.class);
            if (emp.isExpired()) {
                empList.add(emp);
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }

目前我的数据库中有 30 个文档,有没有一种方法可以检索这些文档而不会导致 Firestore 将查询计为 30 次读取但仅计为一次?

    标签: java google-cloud-firestore


    【解决方案1】:

    Firestore 收费文档读取,因此如果您从服务器读取 30 个文档,您将需要为 30 个文档读取付费。

    如果您想阅读更少的文档,请考虑将这 30 个文档中的信息合并到更少的文档中。例如,如果您将“30 个最近的合同”文档添加到集合中,其中包含有关 30 个最近的合同的信息,您只需阅读该文档即可获取信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多