【问题标题】:Can I batch together an add() and a set() operation in cloud Firestore?我可以在云 Firestore 中批量处理 add() 和 set() 操作吗?
【发布时间】:2019-11-04 04:45:36
【问题描述】:

Firebase docs,我们得到:

批量写入

如果您不需要读取操作集中的任何文档,您可以将多个写入操作作为一个批处理执行,其中包含 set()、update() 或 delete() 操作的任意组合。 一批写入原子完成,可以写入多个文档。

但就我而言,我需要确保 add() 操作(创建新文档)将与 set() 操作一起发生以更新一些其他预先存在的文档。

有没有办法做到这一点?

注意:我使用的是 Javascrip SDK。

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    如果你这样做

     const batch = firestore().batch()
     const sampleRef = firestore().collection(‘sample’)
     const id = sampleRef.doc().id
     batch.set(sampleRef.doc(id), {...})
     batch.commit()
    

    它应该做这个把戏,它和add一样

    【讨论】:

    • 谢谢!那可以工作。但到目前为止,我使用的是 Firestore 自动生成的 ID。不知道具体原因,但我正在努力坚持下去。
    • @cbdev420 别怕兄弟,我已经更新了我的答案给你一个firestore自动生成的id,基本上,你可以在提交批处理操作之前预先生成它
    • 太棒了!肯定会尝试的!谢谢
    • 继续粉碎它
    • 有效!解决了我的问题。因为我真的需要提前生成的 ID 用作其他文档中的“外键”。谢谢!
    【解决方案2】:

    只需使用 CollectionReference 的 doc() 方法,然后调用 BatchedWrite 的 set() 方法,以“模仿”对 add() 方法的调用,

    摘自https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#doc

    如果没有指定路径,自动生成的唯一 ID 将用于返回的DocumentReference

    因此,在您的批次中,您可以这样做:

    // Get a new write batch
    var batch = db.batch();
    
    // A "standard" Set
    var nycRef = db.collection("cities").doc("NYC");
    batch.set(nycRef, {name: "New York City"});
    
    // A Set that is similar to an Add
    var unknownCityRef = db.collection("cities").doc();
    batch.set(unknownCityRef, {name: "Unknown City"});
    
    // Commit the batch
    batch.commit().then(function () {
        // ...
    });
    

    【讨论】:

    • 谢谢!我试试看!
    • @cbdev420 请注意,代码中有错字! unknownCityRef 未正确传递给 batch.set()!已更正!
    【解决方案3】:

    为了使用批量写入创建等效于 add() 的新文档,我们需要 首先通过使用 angularfirestore 中的 createID() 函数生成 uid 并设置批处理。

    但是,更新现有文档非常简单。我们只需在设置批处理时提供其 uid。

    `

    constructor(
        private angularFireStore: AngularFirestore,
       ) {}
    
     const batch = this.angularFireStore.firestore.batch();
    
     // generates the unique uid of 20 chars 
     const autogenUid = this.angularFireStore.createId();
     // this is new doc ref with newly generated uid
     const collectionReference = this.angularFireStore.collection
                                ('collection_name').doc(autogenUid).ref;
    
     const docData = {first_field:'value', second_field:'value2'};
     batch.set(collectionReference, docData);
    
     // to update existing , we simply need to know its uid beforehand, 
     const collection2Reference = this.angularFireStore.collection
                                ('collection2_name').doc('existingUid').ref;
    
     const docData2 = {first_field:'value', second_field:'value2'};
     batch.set(collection2Reference, docData2);
    
     batch.commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多