node.js對mongodb的串連&增刪改查(附async同步流程式控制制)

來源:互聯網
上載者:User

標籤:建立   檔案   syn   col   def   lan   失敗   dem   ber   

1.啟動mongodb資料庫

官網下載mongodb資料庫

在mongodb根目錄下建立檔案夾:假設取名為test。 
我們認為test就是mongodb建立的資料庫一枚。

建立批次檔 xxx.bat,內容如下:

運行e盤mongodb檔案夾下bin目錄下的 mongod.exe,參數為 
-dbpath E:\mongodb\test。

E:\mongodb\bin\mongod.exe -dbpath E:\mongodb\test

這樣就啟動了mongodb下test資料庫的伺服器。

2.載入mongodb模組

在我們的node.js項目中直接npm入mongodb模組

npm install mongodb --save

3.依賴mongodb模組

在想要寫對mongodb的增刪改查邏輯的js檔案下加入以下依賴

var mongo = require("mongodb");

4.通用小函數

寫了幾個通用的小函數,最後建立一個對象,將函數全都掛到對象上,最後把對象exports出去即可。

/** * 建立資料庫伺服器並開發名為databaseName的資料庫 * @param host ip * @param port 連接埠 * @param databaseName * @return  開啟失敗返回-1 ,成功返回database */function openDatabase(host,port,databaseName){    //建立資料庫所在的伺服器    var server = new mongo.Server(host, port, {auto_reconnect: true});    var db = new mongo.Db(databaseName, server, {safe: true});    db.open(function (err, db) {        if (err) {            console.log(‘開啟資料庫失敗‘);            return -1;        }        else {            console.log(‘開啟資料庫成功‘);        }    });    return db;}/** * 串連資料集合 * @param db 資料庫 * @param collectionName 資料集合名稱 * @return 成功返回collection,失敗返回-1 */function openCollection(db,collectionName){    db.collection(collectionName,{safe:true},function(errcollection,collection){        if(!errcollection){            console.log(‘串連資料集合成功‘);            return collection;        }else{            console.log(‘串連數集合失敗‘);            return -1;        }    });}/** * 插入資料 * @param collection * @param tmp 要插入的資料 * @return 成功返回collection,失敗返回-1 */function insertCollection(collection,tmp){    //var tmp = {username:‘hello‘,password:1};    collection.insert(tmp,{safe:true},function(err, result){        if(err){            console.log(‘傳入資料集合失敗‘+tmp);            return -1;        }else {            console.log(‘插入資料集合成功‘+result);        }    });    return collection;}/** * 查詢資料集合 沒有條件 * @param collection * @return 成功返回查詢到的資料集合內容,失敗返回-1 */function findCollectionNoCondition(collection){    collection.find().toArray(function(errfind,cols){        if(!errfind){            console.log(‘查詢資料集合成功‘+JSON.stringify(cols));            return JSON.stringify(cols);        }else {            console.log(‘查詢資料集合失敗‘);            return -1;        }    });}/** * 查詢資料集合 有條件 * @param collection * @return 成功返回查詢到的資料集合內容,失敗返回-1 */function findCollectionHasCondition(collection,tmp){    collection.find(tmp).toArray(function(errfind,cols){        if(!errfind){            console.log(‘查詢資料集合成功‘+JSON.stringify(cols));            return JSON.stringify(cols);        }else {            console.log(‘查詢資料集合失敗‘);            return -1;        }    });}/** * 刪除資料集合 * @param collection * @param tmp * @return 成功返回資料集合,失敗返回-1 */function removeCollection(collection,tmp){    //var tmp = {username:‘hello‘,password:1};    collection.remove(tmp,{safe:true},function(err, count){        if(err){            console.log(‘刪除資料集合失敗‘+tmp);            return -1;        }else {            console.log(‘刪除資料集合成功‘+count);            return collection;        }    });}


5.async模組解決node.js非同步架構下同步邏輯的實現

看到《超實用的node.js程式碼片段》這本書上,講到Node.js是非同步I/O驅動,所以在我們順序運行上面的幾個函數的時候,一定會遇到一個問題:如果前面de函數耗時間長度,那麼後面的函數不會等前面的函數運行完,而是直接運行。但是我的前一個函數的運行結果是要被後一個函數使用的呀。

這時候就需要在非同步I/O下進行串列控制流程控制。

書中介紹了async模組,手續愛你第三方引入。

npm install async --save
  • 1

require進來

var async=require("async");
  • 1

使用方法: 
Nodejs非同步流程式控制制Async

我這裡使用的是waterfall瀑布模式流程式控制制 
github waterfall機制使用demo

然後我就天真的使用了

 //使用async瀑布模型流程式控制制執行 資料庫的串連查詢    async.waterfall([        function(callback){            var db;            db=user.openDatabase("localhost",27017,"test");            callback(null,db);        },        function(db,callback){            var collection;            if(db!=-1){                collection=user.openCollection(db,‘users‘);            }            callback(null,collection);        },        function(collection,callback){            var res;            if(collection!=-1){                res=user.findCollectionNoCondition(collection);                console.log(res);                callback(null,3);            }        }    ],function(err,result){        console.log("async瀑布模型流程式控制制執行成功"+result);    })
結果還是第二個函數先於第一個函數運行成功,導致第三個函數不能正確運行。

**********************************************************************
一、使用模組Q的promise機制實現資料庫操作的同步問題

1.install
npm install q --save
2.require
var Q=require(‘q‘);
3.重寫資料庫CRUD操作方法
/** * 建立資料庫伺服器並開發名為databaseName的資料庫 * @param host ip * @param port 連接埠 * @param databaseName * @return  開啟失敗返回-1 ,成功返回database */function openDatabase(host,port,databaseName,collectionName){    //建立資料庫所在的伺服器    var deferred = Q.defer();    var server = new mongo.Server(host, port, {auto_reconnect: true});    var db = new mongo.Db(databaseName, server, {safe: true});    db.open(function (err, db) {        if (err) {            console.log(‘開啟資料庫失敗‘);            deferred.reject(err);        }        else {            console.log(‘開啟資料庫成功‘);            deferred.resolve([db,collectionName]);        }    });    return deferred.promise;}/** * 串連資料集合 * @param db 資料庫 * @param collectionName 資料集合名稱 * @return 成功返回collection,失敗返回-1 */function openCollection(db,collectionName){    var deferred = Q.defer();    db.collection(collectionName,{safe:true},function(errcollection,collection){        if(!errcollection){            console.log(‘串連資料集合成功‘);            deferred.resolve(collection);        }else{            console.log(‘串連數集合失敗‘);            deferred.reject(errcollection);        }    });    return deferred.promise;}/** * 插入資料 * @param collection * @param tmp 要插入的資料 * @return 成功返回collection,失敗返回-1 */function insertCollection(collection,tmp){    //var tmp = {username:‘hello‘,password:1};    collection.insert(tmp,{safe:true},function(err, result){        if(err){            console.log(‘傳入資料集合失敗‘+tmp);            return -1;        }else {            console.log(‘插入資料集合成功‘+result);        }    });    return collection;}/** * 查詢資料集合 沒有條件 * @param collection * @return 成功返回查詢到的資料集合內容,失敗返回-1 */function findCollectionNoCondition(collection){    var deferred = Q.defer();    collection.find().toArray(function(errfind,cols){        if(!errfind){            console.log(‘查詢資料集合成功‘+JSON.stringify(cols));            deferred.resolve(JSON.stringify(cols));        }else {            console.log(‘查詢資料集合失敗‘);            deferred.reject(errfind);        }    });    return deferred.promise;}/** * 查詢資料集合 有條件 * @param collection * @return 成功返回查詢到的資料集合內容,失敗返回-1 */function findCollectionHasCondition(collection,tmp){    collection.find(tmp).toArray(function(errfind,cols){        if(!errfind){            console.log(‘查詢資料集合成功‘+JSON.stringify(cols));            return JSON.stringify(cols);        }else {            console.log(‘查詢資料集合失敗‘);            return -1;        }    });}/** * 刪除資料集合 * @param collection * @param tmp * @return 成功返回資料集合,失敗返回-1 */function removeCollection(collection,tmp){    //var tmp = {username:‘hello‘,password:1};    collection.remove(tmp,{safe:true},function(err, count){        if(err){            console.log(‘刪除資料集合失敗‘+tmp);            return -1;        }else {            console.log(‘刪除資料集合成功‘+count);            return collection;        }    });}

裡面的關鍵點是:

  • 定義defered對象var deferred = Q.defer();
  • 定義函數成功執行的resolve對象deferred.resolve(JSON.stringify(cols));
  • 定義函數成功失敗的error對象 deferred.reject(errfind);
  • 最後返回一個新的promise對象,用來return deferred.promise;
4.鏈式調用
  user.openDatabase("localhost",27017,"test","users")        .then(function(data){            return user.openCollection(data[0],data[1])        })        .then(user.findCollectionNoCondition)        .done(function(data){            console.log(‘promise執行成功‘);        },function(err){           console.log("promise執行失敗:"+err);        });

5.log列印結果
GET / 304 55.922 ms - -開啟資料庫成功串連資料集合成功GET /stylesheets/style.css 304 6.048 ms - -查詢資料集合成功[{"_id":"57731d3a239f75379769ce31","username":"liuchen","password":"12345"},{"_id":"577328ab5c8d6cf43b3e214d","username":"hello","password":1},{"_id":"577331f35c8d6cf43b3e214e","username":"hello","password":1},{"_id":"57733bf400a7090c35122844","username":"hello","password":1},{"_id":"57733c0300a7090c35122845","username":"hello","password":1},{"_id":"57733c0af8d7813443d51c27","username":"hello","password":1}]promise執行成功
















node.js對mongodb的串連&增刪改查(附async同步流程式控制制)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.