nodejs對mongodb數(shù)據(jù)庫的增加修刪該查實(shí)例代碼
以下是實(shí)例代碼:
/** 1.npm install mongodb --save-dev / cnpm install mongodb --save-dev 2.var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/test'; 連接數(shù)據(jù)庫的地址 3.連接數(shù)據(jù)庫 MongoClient.connect(url, function(err, db) { }); 4.實(shí)現(xiàn)增加修改刪除 MongoClient.connect(url, function(err, db) { db.collection('user').insertOne({'name':'zhangsan'},function(error,data){ }) }); */ var http=require('http'); var ejs=require('ejs'); var MongoClient = require('mongodb').MongoClient; /*引入數(shù)據(jù)庫 MongoClient*/ var DBurl = 'mongodb://localhost:27017/userDb'; // 連接數(shù)據(jù)庫的地址 student表示數(shù)據(jù)庫的名稱 var url=require('url'); /*引入url模塊*/ var app=require('./model/express-route.js'); http.createServer(app).listen(3000); app.get('/',function(req,res){ var msg='這是數(shù)據(jù)庫的數(shù)據(jù)' ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){ res.send(data); }) }) app.get('/add',function(req,res){ //增加數(shù)據(jù) MongoClient.connect(DBurl,function(err,db){ /*連接數(shù)據(jù)庫*/ if(err){ console.log(err); console.log('數(shù)據(jù)庫連接失敗'); return; } //增加數(shù)據(jù) db.collection('user').insertOne({ "name":"loaderman", "age":10 },function(error,result){ if(error){ console.log('增加數(shù)據(jù)失敗'); return; } res.send('增加數(shù)據(jù)成功'); db.close();/*關(guān)閉數(shù)據(jù)庫*/ }) }) }) app.get('/edit',function(req,res){ //增加數(shù)據(jù) //res.send('修改數(shù)據(jù)成功'); MongoClient.connect(DBurl,function(err,db){ if(err){ console.log(err); console.log('數(shù)據(jù)庫連接失敗'); return; } db.collection('user').updateOne({"name":"loaderman"},{$set:{ "age":666 }},function(error,data){ if(error){ console.log('修改數(shù)據(jù)失敗'); return; } console.log(data); res.send('修改數(shù)據(jù)成功'); db.close();/*關(guān)閉數(shù)據(jù)庫*/ }) }) }) app.get('/delete',function(req,res){ //增加數(shù)據(jù) //delete?name=lisi //console.log(url.parse(req.url,true)); var query=url.parse(req.url,true).query; //console.log(query.name); var name=query.name; MongoClient.connect(DBurl,function(err,db){ if(err){ console.log(err); console.log('數(shù)據(jù)庫連接失敗'); return; } db.collection('user').deleteOne({"name":name},function(error,data){ if(error){ console.log('刪除失敗'); return; } console.log(data); res.send('刪除數(shù)據(jù)成功'); db.close(); }) }) }) app.get('/query',function(req,res){ MongoClient.connect(DBurl,function(err,db){ if(err){ console.log('連接數(shù)據(jù)庫失敗'); return; } //查詢數(shù)據(jù) var list=[]; /*放數(shù)據(jù)庫里面查詢的所有數(shù)據(jù)*/ var result=db.collection('user').find({}); result.each(function(error,doc){ //console.log(doc); if(error){ console.log(error); }else{ if(doc!=null){ list.push(doc); }else{ /*doc==null表示數(shù)據(jù)循環(huán)完成*/ /*獲取數(shù)據(jù)以后*/ //console.log(list); ejs.renderFile('views/index.ejs',{list:list},function(err,data){ res.send(data); }) } } }) //console.log(result); }) })
插入數(shù)據(jù)
/** * 插入單條數(shù)據(jù) * @param table_name 表名 * @param insertData 插入的數(shù)據(jù) * @param callback 回調(diào)方法 */ MongoDbAction.insertData= function (table_name, insertData , callback) { var node_model = this.getConnection(table_name); node_model.insertOne(insertData , function (err, res) { if (err) { callback(err); } else { callback(null, res); } }); };
查詢數(shù)據(jù)
/** * 查詢單條數(shù)據(jù) * @param table_name 表名 * @param conditions 查詢條件 * @param callback 回調(diào)方法 */ MongoDbAction.findOne = function (table_name, conditions, callback) { var node_model = this.getConnection(table_name); node_model.findOne(conditions, function (err, res) { if (err) { callback(err); } else { callback(null, res); } }); };
更新數(shù)據(jù)
/** * 更新單條數(shù)據(jù) * @param table_name 表名 * @param conditions 查詢條件 {"name":'jackson影琪'}; * @param updateStr 更新數(shù)據(jù) {$set: { "url" : "https://www.cnblogs.com/jackson-zhangjiang" }}; * @param callback 回調(diào)方法 */ MongoDbAction.updateOne= function (table_name, conditions,updateStr , callback) { var node_model = this.getConnection(table_name); node_model.updateOne(conditions,updateStr, function (err, res) { if (err) { callback(err); } else { callback(null, res); } }); };
以上就是本次介紹的全部相關(guān)知識點(diǎn),感謝大家的學(xué)習(xí)。如果有任何補(bǔ)充,可以聯(lián)系小編。
上一篇:JS正則表達(dá)式驗(yàn)證端口范圍(0-65535)
欄 目:JavaScript
下一篇:基于jQuery實(shí)現(xiàn)掛號平臺首頁源碼
本文標(biāo)題:nodejs對mongodb數(shù)據(jù)庫的增加修刪該查實(shí)例代碼
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9305.html
您可能感興趣的文章
- 01-10js判斷一個(gè)對象是數(shù)組(函數(shù))的方法實(shí)例
- 01-10vue中根據(jù)時(shí)間戳判斷對應(yīng)的時(shí)間(今天 昨天 前天)
- 01-10JQuery中的常用事件、對象屬性與使用方法分析
- 01-10解決Vue 刷新頁面導(dǎo)航顯示高亮位置不對問題
- 01-10vue中實(shí)現(xiàn)點(diǎn)擊按鈕滾動到頁面對應(yīng)位置的方法(使用c3平滑屬性實(shí)
- 01-10node.js Promise對象的使用方法實(shí)例分析
- 01-10nodejs開發(fā)一個(gè)最簡單的web服務(wù)器實(shí)例講解
- 01-10JavaScript 變量,數(shù)據(jù)類型基礎(chǔ)實(shí)例詳解【變量、字符串、數(shù)組、對
- 01-10vue 對axios get pust put delete封裝的實(shí)例代碼
- 01-10Nodejs封裝類似express框架的路由實(shí)例詳解


閱讀排行
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?