-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONtoMongo.js
More file actions
50 lines (46 loc) · 1.55 KB
/
JSONtoMongo.js
File metadata and controls
50 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
/*
Import modules/files you may need to correctly run the script.
Make sure to save your DB's uri in the config file, then import it with a require statement!
*/
var fs = require('fs'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
Listing = require('./ListingSchema.js'),
config = require('./config'),
assert = require('assert'),
json = require('./listings.json');
/* Connect to your database */
mongoose.connect(config.db.uri);
var database = mongoose.connection;
database.on('error', console.error.bind(console, 'error connecting')); // added a connection error check
/*
Instantiate a mongoose model for each listing object in the JSON file,
and then save it to your Mongo database
*/
var listing = mongoose.model('Listing', Listing.listingSchema);
var data;
fs.readFile('./listings.json', 'utf8', function read(err, content) {
if(err) {
throw err;
console.log('error reading json file');
}
console.log('data read');
data = JSON.parse(content);
//console.log(data.entries[0]);
//console.log(data.entries.length);
for(var i = 0; i < data.entries.length; i++) {
//console.log(i);
var entry = new listing(data.entries[i]).save(function(err) {
if(err) console.log('file created');
});
}
console.log("json data moved successfully moved to database");
database.close(); //closes the connection when done
});
/*
*/
/*
Once you've written + run the script, check out your MongoLab database to ensure that
it saved everything correctly.
*/