Android-Firebase Firestore-将同一文档写入不同的集合

乔治

我必须将同一文档写到不同的集合中,所以我认为批量写操作对此不利。我想知道firestore数据库中是否有一个函数,我希望用javascript编写,该函数可以复制我编写一次的文档并将其粘贴到其他位置。这些位置应从数据库中的数组中读取。

有人可以帮助我吗?

非常感谢,抱歉我的英语水平不高。

亚历克斯·莫莫

Cloud Firestore中批处理写入用于此确切目的。

您可以作为一个批处理执行多个写操作,其中包含set(),update()或delete()操作的任意组合。一批写操作可以自动完成,并且可以写到多个文档。

作为对您问题的回答,是的,您可以使用此批处理写入操作将同一文档写入不同的集合。

这就是代码中的样子:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
CollectionReference newProductsRef = rootRef.collection("newProductsRef");

WriteBatch writeBatch = rootRef.batch();
batch.set(productsRef.document(), yourObject);
batch.set(newProductsRef.document(), yourObject);

// This how you commit the batch
writeBatch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // ...
    }
});

使用此代码,您将能够将同一yourObject对象写入Cloud Firestore数据库中的两个不同位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 android studio 中使用 firestore 将新集合添加到同一文档

Firebase Firestore 是否计算同一文档的读取次数?

如何在同一文档下检索Firebase Firestore多个字段数据到RecyclerView

在WriteBatch中写入同一文档会导致Firebase中多次写入?

比较同一文档Firestore

在 Firestore 中更新同一文档的 10 个不同字段。顺序重要吗?

Java Android 从 Firestore Firebase 获取集合的名称

在Firebase Firestore(Android)中删除多个文档

Android Firebase Firestore:从Firestore文档的深层获取数据

根据同一文档中的时间戳属性更新Firestore文档上的属性

在Firestore中的集合内查询唯一文档

在Firestore交易中更新同一文档的多个字段时的定价

在 Android Studio 应用之外写入 Firebase Firestore

限制文档字段和竞争条件 - Firebase Firestore Android

Firebase Firestore文档更改

firebase Firestore 文档读取

在 Google Cloud Firestore 中批量更新中删除并重新创建同一文档

Android Studio-在时间限制后中止Firebase Firestore文档写入

减少阅读Firebase Firestore集合中的文档

Firebase Firestore 检查文档是否有集合

Firebase Firestore如何考虑文档写入?

批量写入Firebase Cloud Firestore

不同的userId与服务器将同一文档发布此。

Firebase / Firestore文档未更新

上传文档Firebase Firestore问题

ReactNative Firebase Firestore 在 Android 上崩溃

Xamarin Android Firebase Cloud Firestore错误

在Android上从Firebase Firestore查询对象列表

Firebase Firestore GetInstance在Android上失败