gpt4 book ai didi

firebase - 使用 Firestore 进行多路径更新

转载 作者:行者123 更新时间:2023-12-02 01:06:01 25 4
gpt4 key购买 nike

我读到的有关 Firestore 的任何地方都说它比实时 Firebase 需要更少的非规范化。我猜这是因为它是一个文档数据库,您可以在其中指向特定文档并仅检索该数量的数据(?)。

但是,我想知道如何管理非规范化仍然有用的情况(例如,我们可以通过在其他文档上存储相同的值来将查询保存到包含完整信息数据的文档中)。如果需要更新该值,是否有类似实时 Firebase 多路径更新(更新每个文档的值)来解决此问题?

最佳答案

我想你想说的是'firestore比实时数据库需要更少的非规范化'(两者都是负责存储数据的Firebase产品)'。我认为这种说法不一定正确,因为这完全取决于您的数据架构。 Firestore 强制您遵守一些好的做法,但这并不意味着您可以在实时数据库中获得类似的架构。

更新非规范化数据

您可以使用批量写入来更新位于不同路径的非规范化数据。但是请注意,您一次最多只能更新 500 个实体。

If you do not need to read any documents in your operation set, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.



来自 Firebase Firestore Documentation 的示例
// Get a new write batch
var batch = db.batch();

// Set the value of 'NYC'
var nycRef = db.collection("cities").doc("NYC");
batch.set(nycRef, {name: "New York City"});

// Update the population of 'SF'
var sfRef = db.collection("cities").doc("SF");
batch.update(sfRef, {"population": 1000000});

// Delete the city 'LA'
var laRef = db.collection("cities").doc("LA");
batch.delete(laRef);

// Commit the batch
batch.commit().then(function () {
    // ...
});

注:代码可能不清楚,但在 commit 之前,不会在 Firestore 上执行任何写入操作。方法被调用。

关于firebase - 使用 Firestore 进行多路径更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47499820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com