简体   繁体   中英

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

I have two collections

collection 1- modules,

collection 2- profile >> modules (as a sub collection in profile collection)

I want to copy all documents from collection 1 into collection 2 ( one time operation )

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.

Also, is my approach correct in the first place? What is the proper way to code to achieve this ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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