Created
July 10, 2024 13:27
-
-
Save ShaunSHamilton/f3f855ba795ec920bc61fbb6e2c81e59 to your computer and use it in GitHub Desktop.
MongoDB Performance Test
This file contains 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 { 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