forked from vasansr/pro-mern-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trymongo.js
107 lines (95 loc) · 2.97 KB
/
trymongo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*eslint-disable*/
'use strict';
const MongoClient = require('mongodb');
function testWithCallbacks() {
MongoClient.connect('mongodb://localhost/playground', function(err, db) {
db.collection('employees').insertOne({id: 1, name: 'A. Callback'}, function(err, result) {
console.log("Result of insert:", result.insertedId);
db.collection('employees').find({id: 1}).toArray(function(err, docs) {
console.log('Result of find:', docs);
db.close();
});
});
});
}
function testWithPromises() {
let db;
MongoClient.connect('mongodb://localhost/playground').then(connection => {
db = connection;
return db.collection('employees').insertOne({id: 1, name: 'B. Promises'});
}).then(result => {
console.log("Result of insert:", result.insertedId);
return db.collection('employees').find({id: 1}).toArray();
}).then(docs => {
console.log('Result of find:', docs);
db.close();
}).catch(err => {
console.log('ERROR', err);
});
}
function testWithGenerator() {
const co = require('co');
co(function*() {
const db = yield MongoClient.connect('mongodb://localhost/playground');
const result = yield db.collection('employees').insertOne({id: 1, name: 'C. Generator'});
console.log('Result of insert:', result.insertedId);
const docs = yield db.collection('employees').find({id: 1}).toArray();
console.log('Result of find:', docs);
db.close();
}).catch(err => {
console.log('ERROR', err);
});
}
function testWithAsync() {
const async = require('async');
let db;
async.waterfall([
next => {
MongoClient.connect('mongodb://localhost/playground', next);
},
(connection, next) => {
db = connection;
db.collection('employees').insertOne({id: 1, name: 'D. Async'}, next);
},
(insertResult, next) => {
console.log('Insert result:', insertResult.insertedId);
db.collection('employees').find({id: 1}).toArray(next);
},
(docs, next) => {
console.log('Result of find:', docs);
db.close();
next(null, 'All done');
}
], (err, result) => {
if (err)
console.log('ERROR', err);
else
console.log(result);
});
}
function usage() {
console.log('Usage:');
console.log('node', __filename, '<option>');
console.log('Where option is one of:');
console.log(' callbacks Use the callbacks paradigm');
console.log(' promises Use the Promises paradigm');
console.log(' generator Use the Generator paradigm');
console.log(' async Use the async module');
}
if (process.argv.length < 3) {
console.log("Incorrect number of arguments");
usage();
} else {
if (process.argv[2] === 'callbacks') {
testWithCallbacks();
} else if (process.argv[2] === 'promises') {
testWithPromises();
} else if (process.argv[2] === 'generator') {
testWithGenerator();
} else if (process.argv[2] === 'async') {
testWithAsync();
} else {
console.log("Invalid option:", process.argv[2]);
usage();
}
}