-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb.js
More file actions
47 lines (41 loc) · 1.49 KB
/
db.js
File metadata and controls
47 lines (41 loc) · 1.49 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
/* Set up the MongoDB instance
@jfreeman
*/
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('GrannyDB',server);
exports.init = function() {
//initialize the database
db.open( function(err, db) {
if(!err){
console.log('>>DB open');
console.log('>>Dropping old addresses collection...');
// drop the collection if it exists
db.dropCollection('addresses', function(err, result){
console.log('>> Collection dropped');
});
// Get the collection set up, if not existing already
db.createCollection('addresses', {safe:true}, function(err, collection){
if(!err) {
//do something
console.log('>> Collection Created, DB ready for use');
collection.insert({name:'Freeman, Jonathan', phone:'+1.919.321.0119', address:'345 West Main St, Durham, NC 27701', email:'[email protected]'}, {safe:true}, function( err, result) {
if(err) {
console.log(err);
}
else {
console.log('fake doc added');
}
});
}
else {
console.log('error');
console.log(err);
}
});
}
db.close();
});
};