简体   繁体   English

如何将一个集合作为子集合复制到另一个集合中? Angularfire/ Angular/ Firestore

[英]How do I copy a collection into another, as a sub collection? Angularfire/ Angular/ Firestore

I have two collections我有两个收藏

collection 1- modules,集合 1- 模块,

collection 2- profile >> modules (as a sub collection in profile collection)集合 2- 配置文件 >> 模块(作为配置文件集合中的子集合)

I want to copy all documents from collection 1 into collection 2 ( one time operation )我想将集合 1 中的所有文档复制到集合 2(一次操作)

Below is what I tried以下是我尝试过的

private afs:AngularFirestore;

this.afs.collection('modules').valueChanges().subscribe(vals =>{
        vals.forEach(moduleFromRepo =>{
          this.afs.collection('profiles').doc('userid1').collection('modules').add(moduleFromRepo);    
        })
      });

Problem with this code - Although this works for the first time, and then every time a new document is added to collection 1, the entire collection 1 is re-copied to collection 2, such that I be getting several duplicated document copies in collection 2.这段代码的问题 - 尽管这是第一次有效,然后每次将新文档添加到集合 1 时,整个集合 1 都会重新复制到集合 2,这样我就会在集合 2 中获得多个重复的文档副本.

Also, is my approach correct in the first place?另外,我的方法首先正确吗? What is the proper way to code to achieve this ?实现这一目标的正确编码方法是什么?

我还找到了一种使用 take(1) 的解决方法,它是一个 rxjs 运算符

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

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