Last active
May 17, 2024 08:26
-
-
Save dincosman/60c7fe55f52de6d2392214daee1c1558 to your computer and use it in GitHub Desktop.
One million row insert on FerretDB 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(); | |
| // Mongo API connection string | |
| let url = "mongodb://postgres:postgres@192.168.1.31:27002/ferretdb?authMechanism=PLAIN"; | |
| 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("test") | |
| .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