Skip to content

Instantly share code, notes, and snippets.

@dmeehan1968
Last active March 18, 2018 17:34
Show Gist options
  • Save dmeehan1968/3c776ed7e582930b1ecdb16fc2d3c4d0 to your computer and use it in GitHub Desktop.
Save dmeehan1968/3c776ed7e582930b1ecdb16fc2d3c4d0 to your computer and use it in GitHub Desktop.
CSV to Mongo Stream musings
fs.createReadStream(argv.heart)
.pipe(csv.parse({ objectMode: true }))
.pipe(csv.transform({
header: columns => {
const properties = {
Start: 'start',
End: 'end',
'Heart Rate (count/min)': 'heartRate',
}
return columns.map(column => properties[column] || column)
},
row: row => {
const properties = {
start: date => new Date(date),
end: date => new Date(date),
heartRate: rate => Number(rate),
}
return row.map(({column, value}) => properties[column](value) || value)
},
}))
.pipe(MongoStream.insert({
url: '',
db: '',
collection: '',
}))
.on('init', (collection, next) => {
return collection.drop().then(next)
})
.on('end', (collection, next) => {
return collection.count({}).then(count => console.log(count)).then(next)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment