Nodejs操作MongoDB

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

转载:http://forum.foxera.com/mongo...

先用npm安装mongodb

npm install mongodb

安装成功后,继续在上面操作创建的库和表中操作

插入

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var insertData = function(db, callback) {  
    //连接到表  
    var collection = db.collection('tb2');
    //插入数据
    var data = [{"name":'wilson001',"age":21},{"name":'wilson002',"age":22}];
    collection.insert(data, function(err, result) { 
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }     
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    insertData(db, function(result) {
        console.log(result);
        db.close();
    });
});

查询

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';  
var selectData = function(db, callback) {  
  //连接到表  
  var collection = db.collection('tb2');
  //查询数据
  var whereStr = {"name":'wilson001'};
  collection.find(whereStr).toArray(function(err, result) {
    if(err)
    {
      console.log('Error:'+ err);
      return;
    }     
    callback(result);
  });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
  console.log("连接成功!");
  selectData(db, function(result) {
    console.log(result);
    db.close();
  });
});

修改

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var updateData = function(db, callback) {  
    //连接到表  
    var collection = db.collection('tb2');
    //更新数据
    var whereStr = {"name":'wilson001'};
    var updateStr = {$set: { "age" : 100 }};
    collection.update(whereStr,updateStr, function(err, result) {
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }     
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    updateData(db, function(result) {
        console.log(result);
        db.close();
    });
});

删除

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';  
var delData = function(db, callback) {  
  //连接到表  
  var collection = db.collection('tb2');
  //删除数据
  var whereStr = {"name":'wilson001'};
  collection.remove(whereStr, function(err, result) {
    if(err)
    {
      console.log('Error:'+ err);
      return;
    }     
    callback(result);
  });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
  console.log("连接成功!");
  delData(db, function(result) {
    console.log(result);
    db.close();
  });
});

调用存储过程

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/wilsondb1';    
var invokeProcData = function(db, callback) {  
    //存储过程调用
    db.eval('get_tb2_count()', function(err, result) { 
        if(err)
        {
            console.log('Error:'+ err);
            return;
        }             
        callback(result);
    });
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
    console.log("连接成功!");
    invokeProcData(db, function(result) {
        console.log(result);
        db.close();
    });
});
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看