Last active
February 13, 2024 07:58
-
-
Save alyssaq/10016049 to your computer and use it in GitHub Desktop.
Batch add Documents to 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
| 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