繁体   English   中英

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

[英]Android - Firebase Firestore - Write the same document to different collections

我必须将同一文档写到不同的集合中,所以我认为分批写入对此不利。 我想知道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数据库中的两个不同位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM