MongoDB(二)
七、备份还原
- 备份数据库mongodump
导出语法:mongodump -h -port -u -p -d -o
-h host 服务器IP地址(一般不写 默认本机
-port 端口(一般不写 默认27017
-u user 账号
-p pwd 密码
-d database 数据库(数据库不写则导出全局
-o open 备份到指定目录下
2.还原数据库mongorestore
导出语法:
mongorestore -h -port -u -p -d --drop 备份数据目录
-h host 服务器IP地址(一般不写 默认本机
-port 端口(一般不写 默认27017
-u user 账号
-p pwd 密码
-d database 数据库(数据库不写则导出全局
--drop 备份数据目录
八、mongoose
- 简介
可以通过执行 npm i mongoose
或 yarn add mongoose
的命令安装
- schema
用来约束MongoDB文档数据(哪些字段是必须的,哪些字段可选的)
英文网:http://mongoosejs.com
中文网:http://mongoosejs.net/
model
一个模型对应一个集合
通过模型来管理集合中的数据
- 使用
下面实例可供参考
实例:
// 导入模块
const mongoose=require("mongoose")
// 连接数据库
const db=mongoose.createConnection('mongodb://shop2:admin888@localhost:27017/test2',{useNewUrlParser:true,useUnifiedTopology:true},err =>{
if(err) {
console.log("--------------")
console.log("数据库连接失败:",err)
console.log('---------------')
return
}
console.log("数据库连接成功");
})
// 设置数据类型(声明是那个集合,限制字段个数和字段类型)
const model=db.model('api',{
uname:{type:String,default:"神龙教主"},
pwd:{type:String},
age:{type:Number},
sex:{type:String}
})
// 创建实例操作
const insertObj=new model({ //数据对象
uname:"李四",
pwd:"admin888",
age:18,
sex:"男",
})
insertObj.save()
.then(res=>{
console.log(res)
return res
})
.catch(err =>{
console.log("插入失败"+err)
return false
})
// ------------读----------
model2.find({})
.then(res=>{
console.log(res)
db2.close()
return res
})
.catch(err =>{
console.log(err)
return false
})
// -------分页------
model2.find({}).skip(1).limit(1)
.then(res=>{
console.log(res)
db2.close()
return res
})
.catch(err =>{
console.log(err)
return false
})
九、接口
🎗 1. 概念
{
status:1/0,
msg:"提示信息"
}
--
<xml>
<status>1/0</status>
<msg>提示信息</msg>
</xml>
- 只要响应json数据所有语言都可以操作,
{
meta:{
msg:提示信息,
status:状态码(200/201/301/302/400/401/403/404/500)
},
data:数据
}
🎗2. 作用
数据角度:让我们的项目静态/固态数据动态(也就是让项目数据来源于数据库)
- 为什有接口:
一次编写,多次/随时接入()
- 什么是接口
就是一个文件,但是你必须返回Json或xml数据
- 接口能干吗
(1)-数据角度:页面数据动态 ,
(2)-功能角度:短信接口等
🎗3. 接口使用规范(Restful API)
- Restful是目前最流行的一种互联网软件架构
- 声明或提供了接口设计原则和约束条件
- 相关
- 举例
标准的RESTful架构/思想/规则需要做到
订单模块
/order get
/order post
/order/编号 put
/order/编号 delete
- 项目所有模块有统一的标准
- 看URL就知道要操作的资源是什么(也就是那个模块)
- 看就知道操作的动作是什么,是添加(post)还是删除(delete)
- 看Http Status Code就知道操作结果如何,是成功(200)还是内部错误(500)
🎗 4. 接口测试工具
postman模拟HTTP请求,测试接口,查看接口返回数据
https://www.postman.com/
十、apidoc文档
- 简介
apidoc是nodejs中的一个模块,通过这个模块可以快速生成接口文档,前提是写接口的时候把注释加上
可以在这个链接里下载:https://apidocjs.com/#configuration
- 使用
🧨步骤1:先下载模块,后期通过命令基于注释生成文档(仅一次
npm install apidoc -g
🧨步骤2:在项目根目录创建apidoc.json文件(仅一次
{
"name":"example",
"version":"0.1.0",
"description":"apiDoc basic example",
"title":"Custom apiDoc brower title",
"url":"https://api.github.com/v1"
}
{
"name":"教学管理系统接口文档",
"version":"1.0.0",
"description":"一个接口文档",
"title":"Custom apiDoc brower title",
"url":"http://localhost:3000"
}
🧨步骤3:去写接口注释(n次
/**
* @api {get} /user/:id Request User information
* @apiName GetUser
* @apiGroup User
*
* @apiParam {Number} id Users unique ID.
*
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
🧨步骤四:生成接口文档(N次)
apidoc -i ./接口注释目录 -o ./接口文档存放目录