1. 程式人生 > >nodejs操作mongodb數據庫(mongodb)

nodejs操作mongodb數據庫(mongodb)

title upd water ext require sel fill 存儲過程 ams

1.增加

var MongoClient = require("mongodb").MongoClient;
var DB_URL = "mongodb://localhost:27017/chm";

function insertData(db)
{
    var devices = db.collection(‘vip‘);
    var data = {"name":"node","age":22,"addr":"nb","addTime":new Date()};
    devices.insert(data,function(error, result){
        if(error)
        {
            console.log(‘Error:‘+ error);
        }else{

            console.log(result.result.n);
        }
        db.close();
    });
}

MongoClient.connect(DB_URL, function(error, db){
    console.log(‘連接成功!‘);
    insertData(db);
});

技術分享圖片
2.查找

var mongodb = require(‘mongodb‘)
var MongoClient = require(‘mongodb‘).MongoClient;
var DB_CONN_STR = ‘mongodb://localhost:27017/chm‘;  

var selectData = function(db, callback) {  
  //連接到表  
  var collection = db.collection(‘vip‘);
  //查詢數據
  var whereStr = {"name":‘node‘};
  collection.find(whereStr,function(error, cursor){
    cursor.each(function(error,doc){
        if(doc){
            //console.log(doc);
            if (doc.addTime) {
                console.log("addTime: "+doc.addTime);
            }
        }
    });

  });

}

MongoClient.connect(DB_CONN_STR, function(err, db) {
  console.log("連接成功!");
  selectData(db, function(result) {
    console.log(result);
    db.close();
  });
});

3.更新

var MongoClient = require("mongodb").MongoClient;
var DB_URL = "mongodb://localhost:27017/chm";

MongoClient.connect(DB_URL, function(error, db){
    console.log("連接成功!");
    updateData(db);
});

function updateData(db)
{
    var devices = db.collection(‘vip‘);
    var whereData = {"name":"node"}
    var updateDat = {$set: {"age":26}}; //如果不用$set,替換整條數據
    devices.update(whereData, updateDat, function(error, result){
        if (error) {
            console.log(‘Error:‘+ error);
        }else{
            console.log(result);
        }
        db.close();
    });
}

4.刪除

var MongoClient = require(‘mongodb‘).MongoClient;
var DB_URL = "mongodb://localhost:27017/chm";

MongoClient.connect(DB_URL, function(error, db){
    console.log("連接成功");
    deleteData(db);
});

function deleteData(db)
{
    var devices = db.collection(‘vip‘);
    var data = {"name":"node"};
    devices.remove(data, function(error, result){
        if (error) {
            console.log(‘Error:‘+ error);
        }else{
            console.log(result.result.n);
        }
        db.close();
    })
}

存儲過程

在mongodb中創建存儲過程

技術分享圖片

所有存儲過程都存放在db.system.js中,通過db.eval(“存儲過程ID()”);調用存儲過程。

在代碼中調用:

var MongoClient = require("mongodb").MongoClient;
var DB_URL = "mongodb://localhost:27017/chm";

MongoClient.connect(DB_URL, function(error,db){
    console.log("連接成功!");
    callProcess(db)
});

function callProcess(db)
{
    db.eval("get_vip_count()",function(error, result){
        if (error) {
            console.log(error);
        }else{
            console.log("count:"+result);
        }
        db.close();
    });
}

nodejs操作mongodb數據庫(mongodb)