Cloud Functions: How to copy Firestore Collection to a new document?

Solution 1:

I wrote a small nodejs snippet for this.

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));

Solution 2:

Currently, no. Looping through each document using Cloud Functions and then setting a new document to a different collection with the specified data is the only way to do this. Perhaps this would make a good feature request.

How many documents are we talking about? For something like 10,000 it should only take a few minutes, tops.

Solution 3:

This is the method i use to copy data to another collection, I used it to shift data (like sells or something) from an active collection to a 'sells feed' or 'sells history' collection.

At the top i reference the documents, at the bottom is the quite compact code. You can simply add a for loop on top for more than 1 operation.

Hope it helps somebody :)

DocumentReference copyFrom = FirebaseFirestore.instance.collection('curSells').doc('0001');
DocumentReference copyTo = FirebaseFirestore.instance.collection('sellFeed').doc('0001');

copyFrom.get().then((value) => {
  copyTo.set(value.data())
});

Solution 4:

There is no fast way at the moment. I recommend you rewrite your code like this though:

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());
    })
}