Skip to content

Instantly share code, notes, and snippets.

@hos
Created December 20, 2022 15:35
Show Gist options
  • Save hos/72ba9609625706cd69f5617316d9fe11 to your computer and use it in GitHub Desktop.
Save hos/72ba9609625706cd69f5617316d9fe11 to your computer and use it in GitHub Desktop.
Copy from one firestore collection to another
import admin from "firebase-admin";
import fs from "fs/promises";
const serviceAccount1 = JSON.parse(await fs.readFile("./service-account-1.json"));
const serviceAccount2 = JSON.parse(await fs.readFile("./service-account-2.json"));
const admin1 = admin.initializeApp({ credential: admin.credential.cert(serviceAccount1) }, "one");
const admin2 = admin.initializeApp({ credential: admin.credential.cert(serviceAccount2) }, "two");
const db1 = admin1.firestore();
const db2 = admin2.firestore();
const snapshot = await db1.collection("collectionName").get();
const jsonData = snapshot.docs.map((doc) => doc.data());
const batch2 = db2.batch();
jsonData.forEach((data) => {
const ref = db2.collection("storyKeywords").doc(data.id);
batch2.set(ref, data);
});
await batch2.commit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment