-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb1.js
38 lines (33 loc) · 1.07 KB
/
db1.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
var mongodb = require('mongodb');
var mongodbServer = new mongodb.Server('localhost', 27017, { auto_reconnect: true, poolSize: 10 });
var db = new mongodb.Db('mydb', mongodbServer);
/* open db */
db.open(function() {
/* Select 'contact' collection */
db.collection('contact', function(err, collection) {
/* Insert a data */
collection.insert({
name: 'Fred Chien',
email: '[email protected]',
tel: [
'0926xxx5xx',
'0912xx11xx'
]
}, function(err, data) {
if (data) {
console.log('Successfully Insert');
} else {
console.log('Failed to Insert');
}
});
/* Querying */
collection.find({ name: 'Fred Chien' }, function(err, data) {
/* Found this People */
if (data) {
console.log('Name: ' + data.name + ', email: ' + data.email);
} else {
console.log('Cannot found');
}
});
});
});