Skip to content

Instantly share code, notes, and snippets.

@Neodevils
Created February 3, 2025 22:40
Show Gist options
  • Save Neodevils/d051f03074ccf8f33401dad853b9734e to your computer and use it in GitHub Desktop.
Save Neodevils/d051f03074ccf8f33401dad853b9734e to your computer and use it in GitHub Desktop.
Migrate your JSON files with this code to MongoDB.
import fs from "fs";
import path from "path";
import { MongoClient } from "mongodb";
// Replace these with your own values
const mongoUri = DATABASE_URI_HERE; // MongoDB connection URI
const dbName = "test"; // Database name
const collectionName = "users"; // Collection name
const folderPath = "./data"; // Path to the folder containing JSON files
async function importJsonFilesToMongo() {
// Create a new MongoClient with the appropriate connection URI
const client = new MongoClient(mongoUri);
try {
// Connect to MongoDB
await client.connect();
console.log("Connected to MongoDB");
// Get the database and collection
const db = client.db(dbName);
const collection = db.collection(collectionName);
// Read all files in the folder
const files = fs.readdirSync(folderPath);
// Prepare an array to store bulk insert operations (to insert all data at once)
const bulkOperations = [];
for (const file of files) {
// Ensure the file is a .json file
if (path.extname(file) === ".json") {
const filePath = path.join(folderPath, file);
const fileContent = fs.readFileSync(filePath, "utf-8");
try {
// Parse JSON file
const jsonData = JSON.parse(fileContent);
// Add an insert operation to the bulk array
bulkOperations.push({
insertOne: {
document: jsonData,
},
});
} catch (error) {
console.error(
`Error parsing JSON from file ${file}:`,
error
);
}
}
}
// Perform a bulk write to insert all documents
if (bulkOperations.length > 0) {
const result = await collection.bulkWrite(bulkOperations);
console.log(
`Successfully inserted ${result.insertedCount} documents.`
);
} else {
console.log("No documents to insert.");
}
} catch (error) {
console.error("Error connecting to MongoDB:", error);
} finally {
// Close MongoDB connection
await client.close();
}
}
// Run the import function
importJsonFilesToMongo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment