Created
December 20, 2022 15:35
-
-
Save hos/72ba9609625706cd69f5617316d9fe11 to your computer and use it in GitHub Desktop.
Copy from one firestore collection to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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