①启动mongodb(启动服务器 运行mongo 两个cmd命令框)
vscode新建项目mongodb
新建mongo.js
安装mongodb
cnpm install mongodb --save
进入momgo.js
引入mongodb 远程数据库连接
创建数据库
创建数据表 数据库中没有东西查不到
给表中添加数据
拿表 dbbase.collection(“表名”)
insertone加入一条数据
增加多条数据 insertmany
err数据库异常client.close()数据库关闭
删除数据
单条删除
重复的删除一条
删除多条数据
数据分页
条件查找
模糊查找
②数据渲染 安装ejs mongodb express
router.js
var mongoClient = require("mongodb").MongoClient;
var url = "mongodb://127.0.0.1:27017";
var dbname = "chat";
module.exports = function (app) {
app.get("/", function (req, res) {
mongoClient.connect(url, { useUnifiedTopology: true }, function (error, client) {
if (error) throw error;
var dbbase = client.db(dbname);
dbbase.collection("user").find().toArray(function (err, result) {
if (err) throw err;
console.log(eval(result));
res.render("index", {
data: eval(result)
});
client.close();
});
})
});
}
app.js
var express=require("express")
var router=require("./router")
var app=new express();
app.set("view engine","ejs");
app.use(express.static("static"));
router(app);
app.listen(8100);
index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>首页</div>
<ul>
<%for(var i=0;i<data.length;i++){%>
<li><%=data[i].name%></li>
<%}%>
</ul>
</body>
</html>
③新建文件夹 node服务器+mongodb
安装
新建app.js router.js
app.router方法传app
在router.js引入mongodb
client连接数据库对象
拿表中的数据
将数据渲染到页面上
不是标准格式的json用eval转换
循环绑定 进入index.ejs循环绑定
新文件夹下 路由封装
需要安装express ejs mongodb
新建 mongo/settings.js
新建mongo/mongo.js
把数据库连接写成一个方法
查找方法
为了使查找方法可以关闭数据库 修改连接数据库的代码 把client移出来
有条件查找无条件查找 data
查找路由
增加数据方法
增加数据路由
增加多条数据方法
增加多条数据路由
删除方法
在路由里面调用方法
修改数据方法
修改什么为
数据分页
app.js路由调用方法
var express = require("express");
var app = new express();
var mongo = require("./mongo/mongo");
app.set("view engine", "ejs");
app.use(express.static("static"));
app.get("/", function (req, res) {
mongo.findAll("user", { name: "张三" }, function (result) {
console.log(result);
res.render("index");
});
});
app.get("/insert", function (req, res) {
var data = {
name: "嘿嘿",
sex: "男",
age: 25,
add: "咸阳市"
}
mongo.insertOne("user", data, function (result) {
res.send(result.ok.toString());
})
});
app.get("/insertMany", function (req, res) {
var data = [
{
name: "1",
sex: "男",
age: 25,
add: "?市"
}
]
mongo.insertMany("user", data, function (result) {
res.send(result.ok.toString());
})
});
app.get("/deleteone",function (req,res){
mongo.deleteOne("user",{name:"嘿嘿3"},function (result){
console.log(result);
res.send("删除成功!");
});
});
app.get("/update",function (req,res){
mongo.updateOne("user",{name:"嘿嘿2"},{$set:{sex:"女"}},function (result){
res.send("修改成功!");
});
});
app.get("/page",function (req,res){
mongo.pageNumber("user",4,4,{age:1},function (result){
console.log(result);
res.send("获取数据成功!");
});
});
app.listen(8100);
mongo.js增删改查方法
var mongoClient=require("mongodb").MongoClient;
var setting=require("./settings");
//连接数据库
var connect=function (callback){
mongoClient.connect(setting.url,{ useNewUrlParser: true },function (error,client){
if(error) throw error;
callback(client);
})
}
//查找方法 有无条件都可以使用
exports.findAll=function (tablename,data,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(tablename).find(data).toArray(function (err,result){
if(err) throw err;
callback(result);
client.close();
});
});
}
//增加数据
exports.insertOne=function (name,data,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).insertOne(data,function (err,res){
if(err) throw err;
callback(res.result);
client.close();
});
});
}
//增加多条数据的方法
exports.insertMany=function (name,data,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).insertMany(data,function (err,res){
if(err) throw err;
callback(res.result);
client.close();
});
});
}
//删除方法
exports.deleteOne=function (name,data,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).deleteOne(data,function (err,res){
if(err) throw err;
callback(res);
client.close();
});
});
}
exports.deleteMany=function (name,data,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).deleteMany(data,function (err,res){
if(err) throw err;
callback(res);
client.close();
});
});
}
//修改单条数据
exports.updateOne=function (name,data,set,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).updateOne(data,set,function (err,res){
if(err) throw err;
callback(res);
client.close();
});
});
}
//数据分页
exports.pageNumber=function (name,now,num,sort,callback){
connect(function (client){
var dbname=client.db(setting.dbname);
dbname.collection(name).find().skip(now).limit(num).sort(sort).toArray(function (err,res){
if(err) throw err;
callback(res);
client.close();
});
});
}
setting.js模块暴露
module.exports={
url:"mongodb://127.0.0.1:27017",
dbname:"chat"
}
inde.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
首页
</body>
</html>
④——连接MySQL
新建文件夹 安装ejs express mysql
新建mysql/setting.js
mysql/mysqlclient.js
app.js
var express = require("express");
var app = new express();
var mysql = require("./mysql/mysqlClient");
app.set("view engine", "ejs");
app.get("/", function (req, res) {
mysql.findAll("select * from user where id=?", 2, function (result) {
console.log(result);
res.send("首页");
});
});
app.get("/insert", function (req, res) {
var str = "insert into user (id,name,sex,age,address) values(null,?,?,?,?)";
var params = ["毛豆", "男", 20, "西安市"];
mysql.insertone(str, params, function (result) {
console.log(result);
res.send("增加数据成功!");
});
});
app.listen(8100);
setting.js
module.exports={
host:"localhost",
port:3308,
user:"root",
pwd:"12345678",
dbbase:"student"
}
mysqlclient.js
var mysql = require("mysql");
var setting = require("./setting");
var connection = mysql.createConnection({
host: setting.host,
port: setting.port,
user: setting.user,
password: setting.pwd,
database: setting.dbbase
});
//进行服务器连接
connection.connect();
//查找数据
exports.findAll = function (str, id, callback) {
connection.query(str, id, function (err, res) {
if (err) throw err;
callback(res);
//数据库关闭
connection.end();
});
}
//增加数据
exports.insertone=function(str,params,callback){
connection.query(str,params,function (err,res){
if(err) throw err;
callback(res);
connection.end();
});
}