使用nodejs怎么對(duì)MongoDB數(shù)據(jù)庫進(jìn)行增加修刪該查操作?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
成都創(chuàng)新互聯(lián)專注于武岡網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供武岡營銷型網(wǎng)站建設(shè),武岡網(wǎng)站制作、武岡網(wǎng)頁設(shè)計(jì)、武岡網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造武岡網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供武岡網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
以下是實(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); } }); };
看完上述內(nèi)容,你們掌握使用nodejs怎么對(duì)mongodb數(shù)據(jù)庫進(jìn)行增加修刪該查操作的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
名稱欄目:使用nodejs怎么對(duì)mongodb數(shù)據(jù)庫進(jìn)行增加修刪該查操作
文章來源:http://jinyejixie.com/article4/ijdjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站維護(hù)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)