Skip to content

Instantly share code, notes, and snippets.

@alyssaq
Last active February 13, 2024 07:58
Show Gist options
  • Save alyssaq/10016049 to your computer and use it in GitHub Desktop.
Save alyssaq/10016049 to your computer and use it in GitHub Desktop.
Batch add Documents to Mongodb
var mongoose = require('mongoose');
var DATA = require('../data/dataSources');
var dataSourceSchema = new mongoose.Schema({
sourceid: {type: Number, index: true},
status: {type: String, default: 'active'},
name: String,
provider: {type: String, lowercase: true, default: 'google_adwords'},
updated: {type: Date, default: Date.now}
});
dataSourceSchema.set('autoIndex', false);
DataSource = mongoose.model('DataSource', dataSourceSchema);
// Clear out old data
DataSource.remove({}, function (err) {
if (err) {
console.log('error deleting old data.');
}
});
// Batch add Documents to a Mongodb Collection
DataSource.create(DATA, function (err) {
if (err) {
console.log('Error on save!');
} else {
console.log('Data has been saved')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment