欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

JavaScript

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > JavaScript >

nodejs對mongodb數(shù)據(jù)庫的增加修刪該查實(shí)例代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:JavaScript|點(diǎn)擊: 次

以下是實(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);
    }
  });
};

以上就是本次介紹的全部相關(guān)知識點(diǎn),感謝大家的學(xué)習(xí)。如果有任何補(bǔ)充,可以聯(lián)系小編。

上一篇:JS正則表達(dá)式驗(yàn)證端口范圍(0-65535)

欄    目:JavaScript

下一篇:基于jQuery實(shí)現(xiàn)掛號平臺首頁源碼

本文標(biāo)題:nodejs對mongodb數(shù)據(jù)庫的增加修刪該查實(shí)例代碼

本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9305.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有