-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddPerson.js
51 lines (45 loc) · 1.37 KB
/
addPerson.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
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
//connection url
var url = 'mongodb://localhost:27017/mongoproject';
// use connect method to connect to the server
//
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
insertDocuments(db,function(){
findDocuments(db,function() {
db.close();
});
});
});
var insertDocuments = function(db, callback) {
//get the documetns collection
var collection = db.collection('documents');
// Insert some documents
collection.insertMany([
{
"name":"Barney",
"email":"[email protected]",
"availability": "none",
"previousMatches": [
]
}
], function(err,result) {
assert.equal(err, null);
assert.equal(1,result.result.n);
assert.equal(1, result.ops.length);
console.log("Inserted 1 person into the database");
callback(result);
});
}
var findDocuments = function(db,callback) {
//Get the documents collection
var collection = db.collection('documents');
//Find some documents
collection.find({}).toArray(function(err,docs) {
assert.equal(err,null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
}