node操作mongodb數據庫的封裝

lizhiheng 8年前發布 | 31K 次閱讀 數據庫 MongoDB NoSQL數據庫

最近玩node 的時候,我發現我需要個數據庫,我選擇了mongodb這個數據庫,問了一下度娘基本都是數據庫的基本操作,所以我也就簡單的封裝了幾個操作,以方便我的使用

首先引入mongodb的包

var mongodb = require('mongodb'); // 引入mongodb依賴

引入數據庫配置文件

var dataconfig = require('../config/config'); // 引入mongodb配置

數據庫配置文件

配置文件根據自己的不同需求配置

/* @dataconfig 數據庫配置*/
var dataconfig={
 dataurl:"localhost", 
dataport:'27017',
 dataname:'db'
};
module.exports = dataconfig;

創建對象,并且向其中寫入數據庫操作方法

/*
創建mongodb操作對象
*/
var Mainpulation = {
    /*
    @selectall 方法返回全部所有數據
        @dataname  數據庫名稱
      @dealdata   回調處理函數  格式function(result){};
    */
    selectall: function(dataname, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } 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("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    },
    /*
    @selectone 查詢符合條件的數據
    @dataname 數據庫名稱
    @selectlanguage 查詢控制語句  格式{index:value,index,value};
    @dealdata   回調處理函數  格式function(result){};
    */
    select: function(dataname, selectlanguage, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } 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("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    },
    /*
    @insert添加數據格式json格式
    @dataname 數據庫名稱
    @dealdata 回調函數處理函數有一個result參數
    */
    insert: function(dataname, insertlanguage, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } 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("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    },
    /*
     @update 修改數據的方法
     @update添加數據格式json格式
     @dataname 數據庫名稱
     @dealdata 回調函數處理函數有一個result參數
     */
    update: function(dataname, updatelanguage,updatecondition, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } 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("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    },
    /*
    @remove  刪除數據的方法
    @dataname 數據庫名稱
    @removelanguage 刪除數據的條件
    @dealdata 回調函數處理函數有一個result參數
    */
    remove: function(dataname, removelanguage, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } else {
                        collection.remove(removelanguage, {
                            safe: true
                        }, function(err, result) {
                            dealdata(result);
                            db.close();
                        });
                    }
                })
            }
        })
        db.on("close", function(err, data) {
            if (err) {
                console.log("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    },
    /*
    @remove  刪除全部數據的方法
    @dataname 數據庫名稱
    @dealdata 回調函數處理函數有一個result參數
    */
    removeall: function(dataname, dealdata) {
        var server = new mongodb.Server(dataconfig.dataurl, dataconfig.dataport, {
            auto_reconnect: true
        }); // 創建mongodb服務
        var db = new mongodb.Db(dataconfig.dataname, server, {
            safe: true
        });
        db.open(function(err, db) {
            if (err) {
                console.log('數據庫打開失敗');
            } else {
                db.createCollection(dataname, {
                    safe: true
                }, function(err, collection) {
                    if (err) {
                        console.log('數據庫連接失敗');
                    } else {
                        collection.remove({}, {
                            safe: true
                        }, function(err, result) {
                            dealdata(result);
                            db.close();
                        });
                    }
                })
            }
        })
        db.on("close", function(err, data) {
            if (err) {
                console.log("數據庫關閉失敗");
            }
            console.log('數據庫已經關閉');
        });
    }
};

數據庫封裝完畢,我們測試一下

//測試用例


Mainpulation.select('test', {"title": "hello"}, function(result) {
    console.log("select查詢結果");
    console.log(result);
})
Mainpulation.insert('test',{"insert":"hello"},function(result){
 console.log('inserts插入結果');
  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查詢結果');
    console.log(result);
});

當然最后我們封裝完畢后,我們要把這個模塊導出一下方便我們以后的使用

module.exports = Mainpulation;

 

來自:http://www.jianshu.com/p/d44451117537

 

 本文由用戶 lizhiheng 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!