-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmongodb.js
40 lines (36 loc) · 973 Bytes
/
mongodb.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
const MongoClient = require('mongodb').MongoClient;
var url='mongodb://newuser:[email protected]:51868/music_player';
var obj;
function connectToMongo(run_server){
MongoClient.connect(url,function (err,db) {
if (err) throw err;
console.log("connected to db");
obj=db;
run_server();
// db.close();
});
}
function insertToMongo(item,callback) {
obj.collection('users').insertOne(item,function (err,result) {
if(err) throw err;
callback(result);
});
}
function getList(query,callback) {
obj.collection('users').findOne(query,function (err,result) {
if(err) throw err;
callback(result);
});
}
function updateItem(query,newItem,callback) {
obj.collection('users').updateOne(query,newItem,function (err,result) {
if (err) throw err;
callback(result);
});
}
module.exports = {
connectToMongo,
insertToMongo,
getList,
updateItem
};