Skip to content

Instantly share code, notes, and snippets.

@ShaunSHamilton
Created July 10, 2024 13:27
Show Gist options
  • Save ShaunSHamilton/f3f855ba795ec920bc61fbb6e2c81e59 to your computer and use it in GitHub Desktop.
Save ShaunSHamilton/f3f855ba795ec920bc61fbb6e2c81e59 to your computer and use it in GitHub Desktop.
MongoDB Performance Test
import { MongoClient, type Document } from "mongodb";
const uri =
"<insert_your_uri>";
const client = new MongoClient(uri);
await client.connect();
const database = client.db("freecodecamp");
const usersCollection = database.collection("user");
const usersNewCollection = database.collection("user_new");
const batchSize = 3000;
async function run() {
const cursor = usersCollection.find();
// const cursor = usersCollection.find().batchSize(batchSize);
let batch = [];
for await (const doc of cursor) {
batch.push(doc);
if (batch.length === batchSize) {
const startTime = Date.now();
// await insert(batch);
await bulk(batch);
await insertMany(batch);
const endTime = Date.now();
console.log(`Batch insert completed in ${endTime - startTime} ms`);
batch = [];
}
}
await client.close();
}
async function insertMany(batch: Document[]) {
await usersNewCollection.insertMany(batch);
}
async function bulk(batch: Document[]) {
const bulkOps = batch.map((doc) => {
return {
insertOne: {
document: doc,
},
};
});
await usersNewCollection.bulkWrite(bulkOps);
}
async function insert(batch: Document[]) {
for (const doc of batch) {
await usersNewCollection.insertOne(doc);
}
}
await run();
process.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment