-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.js
51 lines (41 loc) · 1.45 KB
/
operations.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 assert = require('assert');
exports.insertDocument = function(db, document, collection, callback) {
// Get the documents collection
var coll = db.collection(collection);
// Insert some documents
coll.insert(document, function(err, result) {
assert.equal(err, null);
console.log("Inserted " + result.result.n + " documents into the document collection " +
collection);
callback(result);
});
};
exports.findDocuments = function(db, collection, callback) {
// Get the documents collection
var coll = db.collection(collection);
// Find some documents
coll.find({}).toArray(function(err, docs) {
assert.equal(err, null);
callback(docs);
});
};
exports.removeDocument = function(db, document, collection, callback) {
// Get the documents collection
var coll = db.collection(collection);
// Delete the document
coll.deleteOne(document, function(err, result) {
assert.equal(err, null);
console.log("Removed the document " + document);
callback(result);
});
};
exports.updateDocument = function(db, document, update, collection, callback) {
// Get the documents collection
var coll = db.collection(collection);
// Update document
coll.updateOne(document, { $set: update }, null, function(err, result) {
assert.equal(err, null);
console.log("Updated the document with " + update);
callback(result);
});
};