Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment3 #13

Open
wants to merge 4 commits into
base: Assignment3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions JSONtoMongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ 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;
var size = listingData.length;
console.log("size is :"+size);

for(var i = 0;i<size;i++){
var bulidingData = new Listing(listingData[i]);
bulidingData.save(function(err){
if (err) throw err;
/*
Once you've written + run the script, check out your MongoLab database to ensure that
it saved everything correctly.
*/
*/
11 changes: 11 additions & 0 deletions ListingSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
34 changes: 30 additions & 4 deletions queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,53 @@ 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() {
/*
Find the document with the code 'CABL'. This cooresponds with courses that can only be viewed
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();