Created
May 17, 2024 19:20
-
-
Save dincosman/0840b7ecf82789621fb3457af67f142d to your computer and use it in GitHub Desktop.
One million row insert using Oracle Database API for MongoDB Nodejs Script
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
| const fs = require("fs"); | |
| const mongodb = require("mongodb").MongoClient; | |
| const fastimp = require("fast-csv"); | |
| // Start timing | |
| const startTime = process.hrtime(); | |
| // Oracle Database API for MongoDB connection string | |
| let url = "mongodb://insanedba:[email protected]:27003/insanedba?authMechanism=PLAIN&authSource=$external&tlsInsecure=true&ssl=true&retryWrites=false&loadBalanced=true"; | |
| let stream = fs.createReadStream("measurements.txt"); | |
| let statData = []; | |
| let fastream = fastimp | |
| .parse({delimiter: ';'}) | |
| .on("data", function(data) { | |
| statData.push({ | |
| station_name: data[0], | |
| measurement: data[1] | |
| }); | |
| }) | |
| .on("end", function() { | |
| mongodb.connect( | |
| url, | |
| { useNewUrlParser: true, useUnifiedTopology: true}, | |
| (err, client) => { | |
| if (err) throw err; | |
| client | |
| .db("insanedba") | |
| .collection("posts") | |
| .insertMany(statData, (err, res) => { | |
| if (err) throw err; | |
| console.log(`Inserted: ${res.insertedCount} rows`); | |
| // End timing | |
| const endTime = process.hrtime(startTime); | |
| const elapsedSeconds = endTime[0]; | |
| const elapsedNanoseconds = endTime[1]; | |
| const elapsedTimeInMilliseconds = (elapsedSeconds * 1000) + (elapsedNanoseconds / 1000000); | |
| console.log(`Operation took: ${elapsedTimeInMilliseconds} milliseconds`); | |
| client.close(); | |
| }); | |
| } | |
| ); | |
| }); | |
| stream.pipe(fastream); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment