nodejs操作mongodb的增刪改查功能實(shí)例
本文實(shí)例講述了nodejs操作mongodb的增刪改查功能。分享給大家供大家參考,具體如下:
安裝相關(guān)模塊
如果使用這個(gè)的話,你需要先自己安裝一下他需要的模塊,在根目錄輸入
npm install mongodb --save
進(jìn)行模塊安裝,安裝成功以后就可以進(jìn)行以下的步驟。
文件的引入
以下是我書寫的相關(guān)代碼,放到你可以引用的相關(guān)目錄,本人放到了express的根目錄
function Mongo(options) {
this.settings = {
url: 'mongodb://localhost:27017/jk',
MongoClient:require('mongodb').MongoClient,
assert:require('assert')
};
for(let i in options){
this.settings[i] = options[i];
}
this._run = function (fun) {
let that = this;
let settings = this.settings;
this.settings.MongoClient.connect(this.settings.url, function (err, db) {
settings.assert.equal(null, err);
console.log("Connected correctly to server");
fun(db, function () {
db.close();
});
});
};
this.insert = function (collectionName, data, func) {
//增加數(shù)據(jù)
let insertDocuments = function (db, callback) {
let collection = db.collection(collectionName);
collection.insertMany([
data
], function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(insertDocuments);
};
this.update = function (collectionName, updateData, data, func) {
//更新數(shù)據(jù)
let updateDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.updateOne(updateData
, {$set: data}, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(updateDocument);
};
this.delete = function (collectionName, data, func) {
//刪除數(shù)據(jù)
let deleteDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.deleteOne(data, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(deleteDocument);
};
this.find = function (collectionName, data, func) {
//查找數(shù)據(jù)
let findDocuments = function (db, callback) {
// Get the documents collection
let collection = db.collection(collectionName);
// Find some documents
collection.find(data).toArray(function (err, docs) {
if (!err) {
func(true,docs);
}
else {
func(false, err);
}
callback(docs);
});
};
this._run(findDocuments);
};
}
module.exports = Mongo;
我存入到了一個(gè)名字叫server.js的文件名內(nèi)
使用
我們?cè)谛枰褂庙?yè)面先將模塊引入,比如我在路由文件index.js里面引入:
const Server = require("../server.js");
然后需要實(shí)例化對(duì)象,如下:
let server = new Server();
如果需要配置相關(guān)信息,可以在實(shí)例化的時(shí)候傳入一個(gè)對(duì)象配置,可以配置數(shù)據(jù)庫(kù)的地址:
let server = new Server({url:"mongodb://localhost:27017/mydb"});
里面封裝了四個(gè)方法,添刪改查,分別是
添加方法
server.insert(數(shù)據(jù)表名,需要插入的數(shù)據(jù)(鍵值對(duì)的對(duì)象),回調(diào)函數(shù));
更新方法
server.update(數(shù)據(jù)表名,查詢的數(shù)據(jù)(對(duì)象),更新的數(shù)據(jù)(對(duì)象),回調(diào)函數(shù));
刪除方法
server.delete(數(shù)據(jù)表名,查詢的數(shù)據(jù)(對(duì)象),回調(diào)函數(shù));
查找方法
server.find(數(shù)據(jù)表名,查詢的數(shù)據(jù)(對(duì)象),回調(diào)函數(shù));
回調(diào)函數(shù)都會(huì)返回兩個(gè)值,第一個(gè)布爾類型,是否處理成功,第二個(gè)值,查找返回查找到的個(gè)數(shù),別的都返回處理成功的個(gè)數(shù)(現(xiàn)在一次只處理一條)
使用案例
比如我需要在一個(gè)路由里面查找數(shù)據(jù),我就需要這樣:
server.find("users",{username:"username"},function (bool,data) {
if(bool){
console.log("查詢到數(shù)據(jù)為"+data.length+"條");
}
else{
console.log(data);
}
});
});
上面的代碼是查詢了users表里面username為username的字段的數(shù)據(jù),如果成功,后面data就會(huì)返回一個(gè)數(shù)組,如果出現(xiàn)錯(cuò)誤,就直接返回data錯(cuò)誤。
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
nodejs操作mysql實(shí)現(xiàn)增刪改查的實(shí)例
下面小編就為大家?guī)?lái)一篇nodejs操作mysql實(shí)現(xiàn)增刪改查的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
node連接kafka2.0實(shí)現(xiàn)方法示例
這篇文章主要介紹了node連接kafka2.0,nodejs連接kafka2.0的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了kafka2.0的功能、原理、以及node.js連接kafka2.0的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2023-05-05
Node連接mysql數(shù)據(jù)庫(kù)方法介紹
本篇文章主要介紹了Node連接mysql數(shù)據(jù)庫(kù)方法介紹。詳細(xì)的介紹了怎樣連接和操作數(shù)據(jù)庫(kù),并舉例說(shuō)明,有興趣的可以了解一下。2017-02-02
詳解NodeJs項(xiàng)目 CentOs linux服務(wù)器線上部署
這篇文章主要介紹了NodeJs項(xiàng)目 CentOs linux服務(wù)器線上部署,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
nodejs個(gè)人博客開發(fā)第六步 數(shù)據(jù)分頁(yè)
這篇文章主要為大家詳細(xì)介紹了nodejs個(gè)人博客開發(fā)的數(shù)據(jù)分頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Node.js實(shí)現(xiàn)http請(qǐng)求服務(wù)與Mysql數(shù)據(jù)庫(kù)操作方法詳解
這篇文章主要介紹了Node.js實(shí)現(xiàn)http請(qǐng)求服務(wù)與Mysql數(shù)據(jù)庫(kù)操作方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10

