问题描述
我必须将相同的文档写入不同的集合,因此我认为批量写入对此不利。我想知道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.
推荐答案
用于此目的。
作为对您的问题的回答,是的,您可以编写使用批量写入操作的同一文档到不同的集合。
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 - 将同一文档写入不同的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!