android - FireStore 批量写入不同的集合

标签 android firebase google-cloud-firestore

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

有点像官方文档中对多个文档批量写入

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));

是否可以对不同的集合执行批量写入?

最佳答案

批处理操作可以跨集合进行。来自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(),因此您还可以将来自不同集合的文档传入每个调用。

关于android - FireStore 批量写入不同的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126009/

相关文章:

android - 在 toast 上设置背景颜色使其成为矩形

javascript - 如果用户之前使用过该操作,如何让 Dialogflow 代理向用户打招呼?

firebase - 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据?

ios - 如何对字典类型中的特定值求和

java - Android 单选按钮自定义属性

java - 错误:(25, 94)错误:不兼容的类型:SettingsFragment无法转换为Fragment

firebase - Firebase Crashlytics 的 REST API?

angular - 如何在 firestore 中保存日期值

java - 如何使用新的 Android 应用程序更新来更新现有的 SQLite 数据库表数据?

ios - Firebase 推送通知 iOS 问题,无法获取 APNS token 错误域=com.firebase.iid 代码=1001 "(null)"