09. nodejs操作MongoDB

nodejs操作MongoDB

nodejs操作MongoDB上一篇文章对MongoDB的操作是在mongo客户端shell中执行的命令,实际工作中多是利用程序来操作MongDB,类似php操作mysql中的PDO,nodejs操作MongoDB也需要安装相应的依赖,比如mongo、mongose,

mongo库

安装依赖

npm install mongodb --save-dev

在这里插入图片描述

连接数据库

//1. 引入mongo客户端
var mongoClient = require("mongodb").MongoClient;//引入连接客户端
//2. 开始连接
var mongoUrl = "mongodb://localhost:27017/student";//student数据库名
mongo.Client.connect(mongoUrl,{useNewUrlParse:true},function(err,db){
    if(err){
        console.log("连接成功")
    	//其他CRUD操作
    }
})

插入数据

//像student数据库的user集合user插入一条随机记录
db.db("student").collection("user").insertOne({
    name: "user_" + Math.ceil(Math.random() * 100),
    age: Math.ceil(Math.random() * 100),
    sex: Math.random() > 0.5 ? "man" : "woman"
}, function (err, result) {
    if (err) {
        res.end("数据库写入失败");
        return;
    } else {
        if (result.result.ok == 1) {
            res.write("恭喜,数据成功写入");
            res.end();
        } else {
            res.write("遗憾,数据成功失败");
            res.end();
        }
        //最后关闭数据库
        db.close();
    }
})

注意:程序中的db指连接数据库成功后的句柄,下同

查看数据

//查看student数据库中user集合的所有表
db.db("student").collection("user").find().toArray(function (err, result) {
        if (err) {
          res.end("遗憾,数据查询失败");
          console.log("err", err);

        } else {
          console.log("reslut", result);
          res.end("数据查询成功,请前往后台运行界面查看结果");
        }
        db.close();
      });

修改数据

//修改name=happy的文档(表)
db.db("student").collection("user").updateOne({
        "name": "happy" //要替换文档(记录)的检索条件
      }, {//替换目标内容
        $set: {
          name: "happy update ok",
          age: "100",
        }
      }, function (err, result) {
        if (err) {
          res.end("遗憾,数据更新失败");
          console.log("err", err);
        } else {
          console.log("result", result.result.ok);
          res.end("数据更新成功");
        }
        db.close();
      })

删除数据

db.db("student").collection("user").deleteOne({
        name: "zhangsan" //要删除文档(记录)的检索条件
      }, function (err, result) {
        if (err) {
          res.end("遗憾,数据删除失败");
          console.log("err", err);
        } else {
          console.log("result", result.result.ok);
          res.end("数据删除成功");
        }
        db.close();
      })

利用http模块编写简易web服务的完成例程:

var http = require("http");
var url = require("url");
var mongoClient = require("mongodb").MongoClient;//客户端引入
var mongoUrl = "mongodb://localhost:27017/student";// student是数据库名 

var server = http.createServer(function (req, res) {
  var requrl = url.parse(req.url, true);
  var pathname = requrl.pathname;

  res.writeHead(200, { "Content-Type": "text/html;charset=UTF8" });
  if (pathname == "/") {
    res.end("请分别访问 /add、/view、/edit、/delete 来查看CURD效果");
  } else if (pathname == "/add") {
    mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {//数据库连接
      if (err) {
        res.end("数据库连接失败");
        return;
      }
      //像student数据库的user集合user插入一条随机记录
      db.db("student").collection("user").insertOne({
        name: "user_" + Math.ceil(Math.random() * 100),
        age: Math.ceil(Math.random() * 100),
        sex: Math.random() > 0.5 ? "man" : "woman"
      }, function (err, result) {
        if (err) {
          res.end("数据库写入失败");
          return;
        } else {
          if (result.result.ok == 1) {
            res.write("恭喜,数据成功写入");
            res.end();
          } else {
            res.write("遗憾,数据成功失败");
            res.end();
          }
          //最后关闭数据库
          db.close();
        }
      })
    })
  } else if (pathname == "/view") {
    mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
      if (err) {
        res.end("数据库连接错误");
        return;
      }
      console.log("数据库连接成功");
      db.db("student").collection("user").find().toArray(function (err, result) {
        if (err) {
          res.end("遗憾,数据查询失败");
          console.log("err", err);

        } else {
          console.log("reslut", result);
          res.end("数据查询成功,请前往后台运行界面查看结果");
        }
        db.close();
      });
    })
  } else if (pathname == "/edit") {
    mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
      if (err) {
        res.end("数据库连接错误");
        return;
      }
      console.log("数据库连接成功");
      db.db("student").collection("user").updateOne({
        "name": "happy" //要替换文档(记录)的检索条件
      }, {//替换目标内容
        $set: {
          name: "happy update ok",
          age: "100",
        }
      }, function (err, result) {
        if (err) {
          res.end("遗憾,数据更新失败");
          console.log("err", err);
        } else {
          console.log("result", result.result.ok);
          res.end("数据更新成功");
        }
        db.close();
      })
    })
  } else if (pathname == "/delete") {
    mongoClient.connect(mongoUrl, { useNewUrlParser: true }, function (err, db) {
      if (err) {
        res.end("数据库连接错误");
        return;
      }
      console.log("数据库连接成功");
      db.db("student").collection("user").deleteOne({
        name: "zhangsan" //要删除文档(记录)的检索条件
      }, function (err, result) {
        if (err) {
          res.end("遗憾,数据删除失败");
          console.log("err", err);
        } else {
          console.log("result", result.result.ok);
          res.end("数据删除成功");
        }
        db.close();
      })
    })
  }

})
server.listen(9999);
console.log("MongoDB连接服务器例程运行于: http://127.0.0.1:9999");

mongose库

–未完待续–

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章