简体   繁体   English

Firestore — 仅获取大型同步集合中更改的文档

[英]Firestore — Get only the changed documents in a large synced collection

I've read all of the questions below, and cannot find anything in the docs to describe how to sync a collection and receive only changed documents from a collection.我已阅读以下所有问题,但在文档中找不到任何内容来描述如何同步集合并接收来自集合的更改文档。 I've got over 500 documents in my synced collection (using redux-saga-firebase syncCollection ) but usually only about 100 of them ever change.我的同步集合中有超过 500 个文档(使用redux-saga-firebase syncCollection ),但通常只有大约 100 个会更改。 At any given time, even fewer will change, but I'm currently getting all 500+ documents back when even one changes, which results in 500+ reads.在任何给定的时间,甚至更少会发生变化,但我目前正在恢复所有 500 多个文档,即使一个文档发生更改,也会导致 500 多个读取。 Not ideal, and will cost me at scale.不理想,而且会让我付出巨大的代价。

Here is my code using redux-saga-firebase, but a vanilla firebase code answer would also suffice.这是我使用 redux-saga-firebase 的代码,但是一个普通的 firebase 代码答案也足够了。 How can get a synchronized response that sends only the changed documents in the collection?如何获得发送集合中更改的文档的同步响应?

export function* getPlayersFromDb() {
  yield fork(
    rsf.firestore.syncCollection,
    'players',
    { successActionCreator: response => ({
            type: t.GET_PLAYERS_FROM_DB_SUCCESS,
            payload: [...response],
        }),
        failureActionCreator: () => ({
            type: t.GET_PLAYERS_FROM_DB_FAILURE,
            payload: [],
        }),
        transform: response => messageTransformer(response),
    }
);

} }

Does Firestore sync only diff? Firestore 同步是否仅存在差异?

Realm syncing with large collection on Firestore - architectural questions / issues Realm 与 Firestore 上的大型集合同步 - 架构问题/问题

How to avoid unnecessary Firestore reads with Cache 如何使用缓存避免不必要的 Firestore 读取

What exactly does Firestore synchronization do? Firestore 同步到底有什么作用?

Firestore queries don't have a notion of querying "only things that have changed". Firestore 查询没有“仅查询已更改的内容”的概念。 You will need to create that notion on your own.您将需要自己创建该概念。 The usual way to do this is by recording a timestamp in each document with the time it was last updated, then using that field in a range filter to get only documents updated since the latest update was received.执行此操作的常用方法是在每个文档中记录其上次更新时间的时间戳,然后使用范围过滤器中的该字段仅获取自收到最新更新以来更新的文档。 You will need to track that latest update timestamp in the client.您将需要在客户端中跟踪最新的更新时间戳。 One possibility is to use the greatest timestamp seen in any document to date.一种可能性是使用迄今为止在任何文档中看到的最大时间戳。

This is a great article explaining some tips.这是一篇很棒的文章,解释了一些技巧。

A strategy here is to add a property on each document such as lastModified and .get() from the Firebase client cache and only query the server to get documents with a lastModified greater than the client cache document fetch timestamp.这里的一个策略是在每个文档上添加一个属性,例如来自 Firebase 客户端缓存的lastModified.get() ,并且只查询服务器以获取lastModified大于客户端缓存文档获取时间戳的文档。

https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e https://medium.com/firebase-tips-tricks/how-to-drastical-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM