-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONtoMongo.js
80 lines (66 loc) · 1.91 KB
/
JSONtoMongo.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'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'),
listingData = [];
/* Connect to your database using mongoose - remember to keep your key secret*/
mongoose.Promise = global.Promise;
mongoose.connect(config.db.uri, {
keepAlive: true,
reconnectTries: Number.MAX_VALUE,
useMongoClient: true
});
var connection = mongoose.connection;
connection.on('connected', function () {
console.log("Connected to db")
toJSON();
});
connection.on("disconnected", function () {
console.log("Disconnected from db");
});
/*
Instantiate a mongoose model for each listing object in the JSON file,
and then save it to your Mongo database
Remember that we need to read in a file like we did in Bootcamp Assignment #1.
*/
var toJSON = function () {
fs.readFile('./listings.json', 'utf8', function (err, data) {
if (err) {
console.log(err);
return;
}
listingData = JSON.parse(data);
addListings();
});
};
var addListings = function () {
Listing.remove({}, function (err) {
if (err) throw err;
addToDB();
});
};
var addToDB = function () {
for (var obj in listingData["entries"]) {
var entry = new Listing(listingData["entries"][obj]);
entry.save(function (err) {
if (err) {
console.log(err);
}
mongoose.disconnect();
});
};
};
/*
Check to see if it works: Once you've written + run the script, check out your MongoLab database to ensure that
it saved everything correctly.
*/
// Listing.create(listingData.entries, finished);
// function finished(){
// mongoose.disconnect();
// };