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

时间:2020-06-10 23:24:12

标签: javascript firebase google-cloud-firestore

我已经阅读了以下所有问题,并且在文档中找不到任何内容来描述如何同步集合并仅从集合中接收 个更改过的文档。我的同步收藏集中有500多个文档(使用redux-saga-firebase syncCollection),但是通常只有大约100个文档会更改。在任何给定的时间,更改的次数都会更少,但是即使有一个更改,我现在也会得到所有500 + 个文档,从而可以读取500+。不太理想,会花我很多钱。

这是我的代码,使用redux-saga-firebase,但是香草firebase代码答案也足够。如何获得同步响应,该响应仅发送 集合中已更改的文档?

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?

Realm syncing with large collection on Firestore - architectural questions / issues

How to avoid unnecessary Firestore reads with Cache

What exactly does Firestore synchronization do?

2 个答案:

答案 0 :(得分:3)

Firestore查询没有查询“仅已更改的内容”的概念。您将需要自己创建该概念。这样做的通常方法是,在每个文档中记录上次更新时间的时间戳记,然后使用范围过滤器中的该字段仅获取自收到最新更新以来更新的文档。您将需要在客户端中跟踪最新的更新时间戳。一种可能性是使用迄今为止任何文档中看到的最大时间戳。

答案 1 :(得分:0)

这是一篇很好的文章,解释了一些技巧。

这里的策略是在每个文档上添加一个属性,例如来自 Firebase 客户端缓存的 using System; using System.IO; using System.Net; namespace HttpWebRequestConsoleTest { class ConsoleTest { static void Main(string[] args) { byte[] buff = new byte[64 * 1024]; try { HttpWebRequest r = WebRequest.CreateHttp("https://some.invalid.url/endpoint"); r.Method = "POST"; r.AllowWriteStreamBuffering = false; // please do not bufferring r.ContentType = "application/octet-stream"; r.SendChunked = true; using Stream stream = r.GetRequestStream(); for (int i = 0; i < 1000000000; i++) // really, really large stream { stream.Write(buff, 0, buff.Length); //Thread.Sleep(10); } var resp = r.GetResponse(); // ... } catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); // Stream was too long. } } } } lastModified,并且只查询服务器以获取 .get() 大于客户端缓存文档获取时间戳。

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