Android - Firebase Firestore - 将同一文档写入不同的集合 [英] Android - Firebase Firestore - Write the same document to different collections

查看:82
本文介绍了Android - Firebase Firestore - 将同一文档写入不同的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to write the same document to different collections, so I think that batched writes are not good for this. I would like to know if there is a function in firestore database, I expect written in javascript, that can copy a document I write once and paste it to different locations. These locations should be read from an array in the database.

任何人都可以帮助我吗?

Anyone could help me?

非常感谢抱歉我的英语很低。

Thanks a lot and sorry for my low English.

推荐答案

Cloud Firestore中的批量写入用于此目的。


您可以将多个写入操作作为包含set(),update()或delete()操作的任意组合的单个批处理执行。一批写入以原子方式完成,并且可以写入多个文档。

You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.

作为对您的问题的回答,是的,您可以编写使用批量写入操作的同一文档到不同的集合。

And as a response to your question, yes, you can write the same document to different collections using this the batched writes operation.

这在代码中看起来如何:

And this how it looks like in code:

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数据库中的两个不同位置。

Using this code, you'll be able to write the same yourObject object to two different location in your Cloud Firestore database.

这篇关于Android - Firebase Firestore - 将同一文档写入不同的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆