【问题标题】:Cloud Functions: How to copy Firestore Collection to a new document?Cloud Functions:如何将 Firestore 集合复制到新文档?
【发布时间】:2018-09-16 09:20:04
【问题描述】:

我想在使用 Cloud Functions 发生事件时在 Firestore 中制作一个集合的副本

我已经有了这个遍历集合并复制每个文档的代码

const firestore = admin.firestore()
firestore.collection("products").get().then(query => {
  query.forEach(function(doc) {
    var promise = firestore.collection(uid).doc(doc.data().barcode).set(doc.data());
  });
});

有没有更短的版本?一次复制整个集合?

【问题讨论】:

    标签: javascript google-cloud-firestore firebase-admin


    【解决方案1】:

    为此我写了一个小nodejs sn-p。

    const firebaseAdmin = require('firebase-admin');
    const serviceAccount = '../../firebase-service-account-key.json';
    const firebaseUrl = 'https://my-app.firebaseio.com';
    
    firebaseAdmin.initializeApp({
        credential: firebaseAdmin.credential.cert(require(serviceAccount)),
        databaseURL: firebaseUrl
    });
    const firestore = firebaseAdmin.firestore();
    
    async function copyCollection(srcCollectionName, destCollectionName) {
        const documents = await firestore.collection(srcCollectionName).get();
        let writeBatch = firebaseAdmin.firestore().batch();
        const destCollection = firestore.collection(destCollectionName);
        let i = 0;
        for (const doc of documents.docs) {
            writeBatch.set(destCollection.doc(doc.id), doc.data());
            i++;
            if (i > 400) {  // write batch only allows maximum 500 writes per batch
                i = 0;
                console.log('Intermediate committing of batch operation');
                await writeBatch.commit();
                writeBatch = firebaseAdmin.firestore().batch();
            }
        }
        if (i > 0) {
            console.log('Firebase batch operation completed. Doing final committing of batch operation.');
            await writeBatch.commit();
        } else {
            console.log('Firebase batch operation completed.');
        }
    }
    
    copyCollection('customers', 'customers_backup').then(() => console.log('copy complete')).catch(error => console.log('copy failed. ' + error));
    

    【讨论】:

    • 谢谢,这真的很有帮助。这是否也会复制所有子集合数据?
    【解决方案2】:

    目前,没有。使用 Cloud Functions 循环遍历每个文档,然后将新文档设置为具有指定数据的不同集合是执行此操作的唯一方法。也许这会提出一个很好的功能请求。

    我们讨论了多少文件?对于像 10,000 这样的东西,它应该只需要几分钟,顶部。

    【讨论】:

    • 你是怎么计算出来的?我只有 3000 个文档,但我需要它是即时的。
    • 请记住,Firestore 将每个文档保存在多个数据中心中。无法通过分布式数据中心立即复制数千份文档。那里的物理定律对我们不利。
    • 有些人可能会认为这是一个挑战 :) 但严肃地说,这可能会为一些本应是廉价操作的东西带来很多成本。绝对是一个很好的功能请求。
    • 或者我们可以在特定集合上使用 Cloud Firestore 托管导出和导入服务? - cloud.google.com/firestore/docs/manage-data/export-import
    【解决方案3】:

    这是我用来将数据复制到另一个集合的方法,我用它来将数据(如销售或其他东西)从活动集合转移到“销售提要”或“销售历史”集合。

    顶部是我参考的文档,底部是相当紧凑的代码。 您可以简单地在顶部添加一个 for 循环来执行多个操作。

    希望它可以帮助某人:)

    DocumentReference copyFrom = FirebaseFirestore.instance.collection('curSells').doc('0001');
    DocumentReference copyTo = FirebaseFirestore.instance.collection('sellFeed').doc('0001');
    
    copyFrom.get().then((value) => {
      copyTo.set(value.data())
    });
    

    【讨论】:

      【解决方案4】:

      目前没有快速的方法。不过,我建议您像这样重写您的代码:

      import { firestore }  from "firebase-admin";
      async function copyCollection() {
          const products = await firestore().collection("products").get();
          products.forEach(async (doc)=> {
              await firestore().collection(uid).doc(doc.get('barcode')).set(doc.data());
          })
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-16
        • 2021-05-06
        • 2018-05-10
        • 2022-01-19
        • 2020-01-05
        • 1970-01-01
        • 2020-04-04
        相关资源
        最近更新 更多