【问题标题】:FireStore Batched writes on different CollectionsFireStore 批量写入不同的集合
【发布时间】:2018-12-10 01:57:34
【问题描述】:

是否有另一种方法可以对属于各种集合的多个文档执行一组写入?

类似于官方文档中的多个文档上的批量写入

Transactions and batched writes on FireStore Docs

举个例子;

WriteBatch batch = db.batch();

// Set the value of 'NYC' in 'cities' collection

DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, map1);


// Set the value of 'ABC' in 'SomeOtherCollection' collection

DocumentReference otherRef = db.collection("SomeOtherCollection").document("ABC");
batch.set(otherRef,map2));

可能在不同的集合上执行批量写入吗?

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    批处理操作可以跨集合工作。来自documentation on batched writes

    // Get a new write batch
    WriteBatch batch = db.batch();
    
    // Set the value of 'NYC'
    DocumentReference nycRef = db.collection("cities").document("NYC");
    batch.set(nycRef, new City());
    
    // Update the population of 'SF'
    DocumentReference sfRef = db.collection("cities").document("SF");
    batch.update(sfRef, "population", 1000000L);
    
    // Delete the city 'LA'
    DocumentReference laRef = db.collection("cities").document("LA");
    batch.delete(laRef);
    
    // Commit the batch
    batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            // ...
        }
    });
    

    由于您将文档传递给batch.set(),因此您还可以将不同集合中的文档传递给每个调用。

    【讨论】:

    • 只是想知道是否有可能,就像我在示例中展示的那样。
    • 如果文档有这样的例子就好了,因为它没有明确地拼出来。
    猜你喜欢
    • 2021-12-07
    • 2018-08-03
    • 1970-01-01
    • 2020-07-11
    • 2018-08-22
    • 2020-02-26
    • 2021-08-21
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多