From 73a98dbd9d2ee364ab0e3df7aa47722bed5571e1 Mon Sep 17 00:00:00 2001 From: 595456852 <42814114+595456852@users.noreply.github.com> Date: Mon, 17 Sep 2018 23:52:52 -0400 Subject: [PATCH 1/4] Update JSONtoMongo.js --- JSONtoMongo.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/JSONtoMongo.js b/JSONtoMongo.js index f8429e9a..d49e1018 100755 --- a/JSONtoMongo.js +++ b/JSONtoMongo.js @@ -10,14 +10,19 @@ var fs = require('fs'), config = require('./config'); /* Connect to your database */ - +mongoose.connect(config.db.uri); /* Instantiate a mongoose model for each listing object in the JSON file, and then save it to your Mongo database */ +var fs = require('fs'); +var listingData; +fs.readFile('listings.json', 'utf8', function(err, data) { + if (err) throw err; + listingData = JSON.parse(data).entries; /* Once you've written + run the script, check out your MongoLab database to ensure that it saved everything correctly. - */ \ No newline at end of file + */ From ad2239c83a0afc3897c55f41c7c266996ec30967 Mon Sep 17 00:00:00 2001 From: 595456852 <42814114+595456852@users.noreply.github.com> Date: Mon, 17 Sep 2018 23:58:25 -0400 Subject: [PATCH 2/4] Update JSONtoMongo.js --- JSONtoMongo.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/JSONtoMongo.js b/JSONtoMongo.js index d49e1018..bf50fe1b 100755 --- a/JSONtoMongo.js +++ b/JSONtoMongo.js @@ -21,7 +21,13 @@ fs.readFile('listings.json', 'utf8', function(err, data) { if (err) throw err; listingData = JSON.parse(data).entries; + var size = listingData.length; + console.log("size is :"+size); + for(var i = 0;i Date: Tue, 18 Sep 2018 23:13:07 -0400 Subject: [PATCH 3/4] Update ListingSchema.js --- ListingSchema.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ListingSchema.js b/ListingSchema.js index 7f48c6cb..70f1eac9 100755 --- a/ListingSchema.js +++ b/ListingSchema.js @@ -5,11 +5,22 @@ var mongoose = require('mongoose'), /* Create your schema */ var listingSchema = new Schema({ /* your code here */ + code: {type: String, required: true}, + name: {type: String, required: true}, + coordinates: {latitude: Number, longitude: Number}, + address: String, + created_at: Date, + updated_at: Date }); /* create a 'pre' function that adds the updated_at (and created_at if not already there) property */ listingSchema.pre('save', function(next) { /* your code here */ + this.updated_at = new Date(); + if (!this.created_at){ + this.created_at = this.updated_at; + } + next(); }); /* Use your schema to instantiate a Mongoose model */ From 44d963cbab36855c19a140ca9453136b12afe637 Mon Sep 17 00:00:00 2001 From: 595456852 <42814114+595456852@users.noreply.github.com> Date: Tue, 18 Sep 2018 23:24:15 -0400 Subject: [PATCH 4/4] Update queries.js --- queries.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/queries.js b/queries.js index 09af6d32..08f09c0d 100755 --- a/queries.js +++ b/queries.js @@ -5,6 +5,11 @@ var findLibraryWest = function() { Find the document that contains data corresponding to Library West, then log it to the console. */ + Listing.find({ code: 'LBWEST' , name : 'Library West'}, function(err,listing) { + if (err) throw err; + + console.log(listing); + }); }; var removeCable = function() { /* @@ -12,20 +17,41 @@ var removeCable = function() { on cable TV. Since we live in the 21st century and most courses are now web based, go ahead and remove this listing from your database and log the document to the console. */ + Listing.findOneAndRemove({ code : 'CABL' }, function(err) { + if (err) throw err; + + console.log('CABL deleted!'); + }); }; -var updatePhelpsLab = function() { - /* - Phelps Laboratory's address is incorrect. Find the listing, update it, and then +var updatePhelpsMemorial = function() { + /* + Phelps Memorial Hospital Center's address is incorrect. Find the listing, update it, and then log the updated document to the console. */ + Listing.find({name : 'Phelps Memorial Hospital Center'}, function(err, listing) { + if (err) throw err; + + listing.address = 'Gainesville, FL'; + + listing.save(function(err) { + if (err) throw err; + + console.log('Listing successfully updated!'); + }); + }); }; var retrieveAllListings = function() { /* Retrieve all listings in the database, and log them to the console. */ + Listing.find( {}, function(err, listings) { + if (err) throw err; + + console.log(listings); + }); }; findLibraryWest(); removeCable(); -updatePhelpsLab(); +updatePhelpsMemorial(); retrieveAllListings();