node操作mongodb數(shù)據(jù)庫(kù)的封裝
最近玩node 的時(shí)候,我發(fā)現(xiàn)我需要個(gè)數(shù)據(jù)庫(kù)近迁,我選擇了mongodb這個(gè)數(shù)據(jù)庫(kù),我也就簡(jiǎn)單的封裝了幾個(gè)操作,以方便的使用
首先引入mongodb的包
var mongodb = require('mongodb'); // 引入mongodb依賴```
**引入數(shù)據(jù)庫(kù)配置文件**
```javascript
var dataconfig = require('../config/config'); // 引入mongodb配置```
**數(shù)據(jù)庫(kù)配置文件**
配置文件根據(jù)自己的不同需求配置
```javascript
/* @dataconfig 數(shù)據(jù)庫(kù)配置*/
var dataconfig={
dataurl:"localhost",
dataport:'27017',
dataname:'db'
};
module.exports = dataconfig;```
**創(chuàng)建對(duì)象垂睬,并且向其中寫入數(shù)據(jù)庫(kù)操作方法**
```javascript
/*
創(chuàng)建mongodb操作對(duì)象
*/
var Mainpulation = {
/*
@selectall 方法返回全部所有數(shù)據(jù)
@dataname 數(shù)據(jù)庫(kù)名稱
@dealdata 回調(diào)處理函數(shù) 格式function(result){};
*/
selectall: function(dataname, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
// var tmp1 = {title:'hellodsad'};
// var tmp2 = {title:'worlddsad'};
// collection.insert([tmp1,tmp2],{safe:true},function(err,result){
// console.log(result);
// });
collection.find().toArray(function(err, docs) {
console.log('find');
dealdata(docs);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
},
/*
@selectone 查詢符合條件的數(shù)據(jù)
@dataname 數(shù)據(jù)庫(kù)名稱
@selectlanguage 查詢控制語(yǔ)句 格式{index:value,index,value};
@dealdata 回調(diào)處理函數(shù) 格式function(result){};
*/
select: function(dataname, selectlanguage, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
collection.find(selectlanguage).toArray(function(err, docs) {
console.log('find');
dealdata(docs);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
},
/*
@insert添加數(shù)據(jù)格式j(luò)son格式
@dataname 數(shù)據(jù)庫(kù)名稱
@dealdata 回調(diào)函數(shù)處理函數(shù)有一個(gè)result參數(shù)
*/
insert: function(dataname, insertlanguage, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
collection.insert(insertlanguage, {
safe: true
}, function(err, result) {
console.log(result+'插入成功');
dealdata(result);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
},
/*
@update 修改數(shù)據(jù)的方法
@update添加數(shù)據(jù)格式j(luò)son格式
@dataname 數(shù)據(jù)庫(kù)名稱
@dealdata 回調(diào)函數(shù)處理函數(shù)有一個(gè)result參數(shù)
*/
update: function(dataname, updatelanguage,updatecondition, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
collection.update(updatecondition,updatelanguage, {
safe: true
}, function(err, result) {
console.log(result+'修改成功');
dealdata(result);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
},
/*
@remove 刪除數(shù)據(jù)的方法
@dataname 數(shù)據(jù)庫(kù)名稱
@removelanguage 刪除數(shù)據(jù)的條件
@dealdata 回調(diào)函數(shù)處理函數(shù)有一個(gè)result參數(shù)
*/
remove: function(dataname, removelanguage, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
collection.remove(removelanguage, {
safe: true
}, function(err, result) {
dealdata(result);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
},
/*
@remove 刪除全部數(shù)據(jù)的方法
@dataname 數(shù)據(jù)庫(kù)名稱
@dealdata 回調(diào)函數(shù)處理函數(shù)有一個(gè)result參數(shù)
*/
removeall: function(dataname, dealdata) {
var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
auto_reconnect: true
}); // 創(chuàng)建mongodb服務(wù)
var db = new mongodb.Db(dataconfig.dataname, server, {
safe: true
});
db.open(function(err, db) {
if (err) {
console.log('數(shù)據(jù)庫(kù)打開失敗');
} else {
db.createCollection(dataname, {
safe: true
}, function(err, collection) {
if (err) {
console.log('數(shù)據(jù)庫(kù)連接失敗');
} else {
collection.remove({}, {
safe: true
}, function(err, result) {
dealdata(result);
db.close();
});
}
})
}
})
db.on("close", function(err, data) {
if (err) {
console.log("數(shù)據(jù)庫(kù)關(guān)閉失敗");
}
console.log('數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉');
});
}
};
數(shù)據(jù)庫(kù)封裝完畢,我們測(cè)試一下
//測(cè)試用例
Mainpulation.select('test', {"title": "hello"}, function(result) {
console.log("select查詢結(jié)果");
console.log(result);
})
Mainpulation.insert('test',{"insert":"hello"},function(result){
console.log('inserts插入結(jié)果');
console.log(result);
})
Mainpulation.remove('test',{"insert":"hello"},function(result){
console.log(result);
})
Mainpulation.removeall('test',function(result){
console.log(result);
})
Mainpulation.selectall('test', function(result) {
console.log('selectall查詢結(jié)果');
console.log(result);
});
當(dāng)然最后我們封裝完畢后,我們要把這個(gè)模塊導(dǎo)出一下方便我們以后的使用
module.exports = Mainpulation;```