-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
46 lines (40 loc) · 1.24 KB
/
database.js
File metadata and controls
46 lines (40 loc) · 1.24 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
/**
* @author cgriffin
*/
var mongoose = require("mongoose");
// BELOW is to connect to running MongoDB server
databaseUrl = "dbh26.mongolab.com:27267";
databaseName = "nodejs";
username = "cgriffin4";
password = "Ifor1loveDaisy!";
var loginCredentials = username + ":" + password;
var dbUrl = databaseUrl;
var dbName = databaseName;
//
// Opens connection to MongoDB database, authenticates, logs successful connection.
//
function initializeDb() {
mongoose.connection.on("open", function() {
console.log("Connected to MongoDB successfully!");});
mongoose.connect("mongodb://" + loginCredentials + "@" + dbUrl + "/" + dbName);
}
//
// Queries a MongoDB collection to retrieve data based on
// properties supplied by json parameter.
//
function query (collectionIdent, json, callback) {
mongoose.connection.db.collection(collectionIdent, function (err, collection) {
collection.find(json).toArray(callback);
});
}
//
// Inserts into a MongoDB collection and returns inserted data
//
function insert (collectionIdent, json, callback) {
mongoose.connection.db.collection(collectionIdent, function (err, collection) {
collection.insert(json);
});
}
exports.start = initializeDb;
exports.get = query;
exports.put = insert;