-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.js
More file actions
76 lines (63 loc) · 2.12 KB
/
queries.js
File metadata and controls
76 lines (63 loc) · 2.12 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
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
/* Fill out these functions using Mongoose queries*/
var
mongoose = require('mongoose'),
Schema = mongoose.Schema,
Listing = require('./ListingSchema.js'),
config = require('./config'),
assert = require('assert'),
json = require('./listings.json');
mongoose.connect(config.db.uri);
var database = mongoose.connection;
database.on('error', console.error.bind(console, 'error connecting')); // added a connection error check
var listing = mongoose.model('Listing', Listing.listingSchema);
var findLibraryWest = function() {
/*
Find the document that contains data corresponding to Library West,
then log it to the console.
*/
listing.find({ name: 'Library West' }, function(err, name) {
if (err) throw err;
console.log(name);
});
};
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.find({ code: 'CABL' }, function(err, name) {
if (err) throw err;
console.log(name);
});
listing.findOneAndRemove({ code: 'CABL' }, function(err) {
if (err) throw err;
// CABL deleted
console.log('CABL deleted!');
});
};
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.findOneAndUpdate({ name: 'Phelps Laboratory' }, { address: '102 Phelps Lab Gainesville, FL 32611' }, function(err, user) {
if (err) throw err;
// Address updated
console.log(user);
});
};
var retrieveAllListings = function() {
/*
Retrieve all listings in the database, and log them to the console.
*/
listing.find({}, function(err, all) {
if (err) throw err;
// all users
console.log(all);
});
};
findLibraryWest();
removeCable();
updatePhelpsMemorial();
retrieveAllListings();