Last active
February 20, 2019 01:37
-
-
Save dgadiraju/9cbb4bed21a5e13a759c5a54a5b8c311 to your computer and use it in GitHub Desktop.
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
const mongoose = require('mongoose'); | |
const fs = require('fs'); | |
const mongoUrl = 'mongodb://localhost:27017/retail'; | |
mongoose.connect(mongoUrl, { useNewUrlParser: true }); | |
const OrderSchema = mongoose | |
.Schema({ | |
order_id: { type: Number, required: true, unique: true }, | |
order_date: { type: String, required: true }, | |
order_customer_id: { type: Number, required: true }, | |
order_status: { type: String, required: true } | |
}); | |
const OrderModel = mongoose.model('order', OrderSchema, 'orders'); | |
fs.readFile('/Users/itversity/Research/data/retail_db/orders/part-00000', (err, data) => { | |
const orders = data | |
.toString() | |
.split('\n') | |
.map((order) => { | |
const o = order.split(','); | |
return OrderModel({ | |
order_id: parseInt(o[0], 0), | |
order_date: o[1], | |
order_customer_id: parseInt(o[2], 0), | |
order_status: o[3] | |
}); | |
}); | |
OrderModel | |
.collection | |
.insert(orders) | |
.then((dataInserted) => { | |
mongoose.connection.close(); | |
console.log(dataInserted.result.n); | |
}) | |
.catch((insertErr) => { | |
mongoose.connection.close(); | |
throw insertErr; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment