Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const mongoClient = require('mongodb').MongoClient
- const databaseInfo = {
- host: 'host',
- port: 'port',
- user: 'username',
- pass: 'password',
- db: 'database',
- collection: 'collection',
- get url () {
- return `mongodb://${this.user}:${this.pass}@${this.host}:${this.port}/${this.db}`
- }
- }
- const MONGO_URL = databaseInfo.url
- const COLLECTION = databaseInfo.collection
- const insertDocuments = (db, data, callback) => {
- const collection = db.collection(COLLECTION)
- collection.insertMany(data, (err, result) => {
- if (err) {
- throw err
- }
- console.log('THE DATA HAS BEEN SAVED.')
- callback(result)
- })
- }
- mongoClient.connect(MONGO_URL, (err, db) => {
- if (err) {
- throw err
- }
- fs.readFile('./sample.json', 'utf8', (err, data) => {
- if (err) {
- throw err
- }
- insertDocuments(db, JSON.parse(data), () => {
- db.close()
- })
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement