Created
January 17, 2019 18:12
-
-
Save muhammadfaizan/e3fb6f53922d950cb1a04ed02f057cc7 to your computer and use it in GitHub Desktop.
Creating Migrations with Bulk upload in mongoDB
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
require('dotenv').config() | |
const { MongoClient, ObjectID } = require('mongodb') | |
const url = process.env.MASTER_DB_URI | |
const documents = require('./data.json') | |
const connect = dbUrl => new Promise((resolve, reject) => { | |
MongoClient.connect(dbUrl, (err, db) => { | |
if (err) reject(err) | |
else resolve(db) | |
}) | |
}) | |
const createCollection = (db, collectionName) => new Promise((resolve, reject) => { | |
db.createCollection(collectionName, (err, collection) => { | |
if (err) reject(err) | |
else resolve(collection) | |
}) | |
}) | |
const task = (db, collection, docs) => { | |
if (!db || !collection || !docs) { | |
throw new Error('fields missing') | |
} | |
const bulk = collection.initializeUnorderedBulkOp() | |
docs.forEach((_doc) => { | |
const doc = _doc | |
if (doc._id && typeof doc._id === 'string') { | |
doc._id = ObjectID(doc._id) | |
} | |
if (doc.parentSpecialties) { | |
doc.parents = doc.parents.map(id => ObjectID(id)) | |
} | |
bulk.insert(doc) | |
}) | |
return new Promise((resolve, reject) => { | |
bulk.execute((err, result) => { | |
if (err) reject(err) | |
else resolve(result) | |
}) | |
}) | |
} | |
exports.up = async function up (next) { | |
try { | |
const db = await connect(url) | |
const collection = await createCollection(db, 'items') | |
await task(db, collection, documents) | |
next() | |
} catch (err) { | |
throw err | |
} | |
} | |
exports.down = function down (next) { | |
next() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment