-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo.js
More file actions
41 lines (36 loc) · 1021 Bytes
/
mongo.js
File metadata and controls
41 lines (36 loc) · 1021 Bytes
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
var MongoClient = require('mongodb').MongoClient
exports.connect = function() {
// Connection URL
var url = process.env.DB_URL || 'mongodb://localhost:27017/myproject';
console.log('Using db:' + url);
return MongoClient.connect(url);
}
exports.close = function(db) {
if (db) {
db.close(function(err, result) {
db = null
mode = null
done(err)
})
}
}
exports.insertStock = function(db, data) {
var collection = db.collection('books_msz');
collection.updateOne( {'isbn': data.isbn}, data, { upsert: true}).then(function(data) {
return data;
});
}
exports.findAll = function(db, callback) {
var collection = db.collection('books_msz');
collection.find({}).toArray(function(err, docs) {
console.log("Found the following records");
console.dir(docs)
callback(docs);
});
}
exports.findStockByIsbn = function(db, id) {
var collection = db.collection('books_msz');
return collection.findOne( { "isbn": id }).then(function(data) {
return data;
});
}