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

Khaled picture Khaled · Apr 6, 2018 · Viewed 16.3k times · Source

I'd like to make a copy of a collection in Firestore upon an event using Cloud Functions

I already have this code that iterates over the collection and copies each document

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

is there a shorter version? to just copy the whole collection at once?

Answer

rayfarer picture rayfarer · Apr 6, 2018

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.