forked from garystafford/meanstack-data-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo-setup.js
60 lines (54 loc) · 1.59 KB
/
mongo-setup.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
// set up =====================================
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var database = 'meanstack-test';
var db = new Db(database, new Server('localhost', 27017), {w: 1});
var newCollection = 'components';
var collection = {};
db.open(function (err, db) {
if (err) {
console.error(err.message);
return false;
}
// drop collection if it already exists
db.dropCollection(newCollection, function () {
console.log('collection dropped: ' + newCollection);
}());
// instantiate new collection
var collection = db.collection(newCollection);
console.log('collection created: ' + newCollection);
// populate new collection
collection.insert([
{
'component': 'mongod',
'description': 'core database process'
},
{
'component': 'mongos',
'description': 'controller and query router for sharded clusters'
},
{
'component': 'mongo',
'description': 'interactive MongoDB Shell'
},
{
'component': 'mongodump',
'description': 'utility for creating binary export of database contents'
},
{
'component': 'mongorestore',
'description': 'writes data from a binary database dump to a MongoDB instance'
},
{
'component': 'mongooplog',
'description': 'polls operations from the replication oplog'
}
], function (err, result) {
if (err) {
console.error(err.message);
return db.close();
}
console.log('documents created: ' + result.length);
return db.close();
});
});