Installation mongdb
Check the mongdb command: which mongod
Start mongdb Services: mongod
mongoose Chinese Documents
https://mongoose.shujuwajue.com/
Mongoose mounted in the demo: npm i mongoose --save
demo path
Configure database connection address mongdb in config.js dbs folder
The basic syntax: object, is connected to a database field dbs address: dbs: Address Database
module.exports = {
dbs:'mongodb://127.0.0.1:27017/dbs'//连接本地数据库
}
Mongdb configuration data format in person.js dbs of modules folder, create a table model
A basic syntax (solid column table to create a data format): new mongoose.Schema ({field name: field type, the field name: field type})
The basic syntax of two (to create a model of a table): mongoose.model (table name, column data format real)
const mongoose = require('mongoose')
let personSchema = new mongoose.Schema({//设计一个personSchema的数据格式
name:String,
age:Number
})
module.exports = mongoose.model('Person',personSchema)//创建一个mongoose的Person模型表
App.js introduced in database processing and database configuration and connected
The basic syntax (connection database): mongoose.connect (address of the database to be connected, {userNewUrlParser: true}) // object for which the configuration database
//引入数据库处理和配置
const mongoose = require('mongoose')
const dbConfig = require('./dbs/config')
//连接数据库
mongoose.connect(dbConfig.dbs,{
userNewUrlParser:true
})
Operation (add, delete, change, search) in the database folder users.js routers in
1, the introduction of real-column table creation realization of additions and deletions to change search mongoose
//引入创建表的实列 实现mongoose的增删改查
const Person = require('../dbs/modules/person')
//路由添加前缀 localhost:8080/users/
router.prefix('/users')
2, by writing data to the database
The basic syntax (by): new new table name ({Field Name: a data field name: two data})
The basic syntax (save): database name .save ()
//将数据写入数据库
router.post('/addPerson',async (ctx)=>{//通过在控制台输入指令:curl -d 'name=faker&age=20' http://localhost:8080/users/addPerson
const person = new Person({//给表填入对应的数据
name:ctx.request.body.name,
age:ctx.request.body.age
})
let code
try {
await person.save()//存储数据
code = 0
}catch (e) {
code = -1
}
ctx.body={code}
})
3, investigation, read the contents of the database
1 basic syntax (a lookup returns a first Found): table name (table object) .findOne ({field name: field data corresponding to the field name 2: Field data corresponding 2}) // findOne inside the object conditions for the data you want to query;
2 basic syntax (find all eligible): table name (table object) .find ({condition})
//读取数据库中的内容
router.post('/getPerson',async (ctx)=>{//$ curl -d 'name=faker' http://localhost:8080/users/getPerson
const result = await Person.findOne({name:ctx.request.body.name})//找到数据据中的一个数据
const results = await Person.find({name:ctx.request.body.name})//找到数据库中复合条件的所有数据
ctx.body = {
code:0,
result,
results
}
})
4, change, update data
Basic syntax: table name (table object) .where ({
Field Name 1: 1 field corresponding to the data,
2 Field Name: field data corresponding 2
}).update({
Field Name: Field updated content
})
简单说:表名称.where({更新的条件}).uodate({更新的内容})
//更新数据
router.post('/updatePerson',async (ctx)=>{// curl -d 'name=marin&age=312' http://localhost:8080/users/updatePerson
const result = await Person.where({//根据where中的条件,去改变那对应的数据
name:ctx.request.body.name
}).update({
age:ctx.request.body.age
})
ctx.body={
code:0
}
})
5、删,删除数据
基本语法:表名称.where({删除数据的条件}).remove()
//删除数据
router.post('/removePerson',async (ctx)=>{
const result = await Person.where({
name:ctx.request.body.name,
age:ctx.request.body.age
}).remove()
ctx.body={
code:0
}
})