-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
84 lines (74 loc) · 2.37 KB
/
queries.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
81
82
83
84
/* Add all the required libraries*/
var mongoose = require('mongoose'),
Listing = require('./ListingSchema'),
config = require('./config');
/* 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")
// findLibraryWest();
// removeCable();
// updatePhelpsMemorial();
// retrieveAllListings();
});
connection.on("disconnected", function () {
console.log("Disconnected from db");
});
/* Fill out these functions using Mongoose queries*/
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, value) {
if (err) throw err;
console.log(value);
});
};
var removeCable = function() {
/*
Find the document with the code 'CABL'. This corresponds 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 removed successfully!');
retrieveAllListings();
});
};
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, data) {
if (err) throw err;
});
Listing.find({name: "Phelps Laboratory"}, function (err, value) {
if (err) throw err;
console.log(value);
});
};
var retrieveAllListings = function() {
/*
Retrieve all listings in the database, and log them to the console.
*/
Listing.find({}, function (err, data) {
if (err) throw err;
const util = require('util')
console.log(util.inspect(data, {maxArrayLength: null}));
// console.log(data)
});
};
findLibraryWest();
removeCable();
updatePhelpsMemorial();
retrieveAllListings();
// mongoose.disconnect();