Vue全家桶+SSR+Koa2全栈开发美团网③——mongoose基础

先安装mongoDB,启动数据库

然后安装mongoDB可视化数据管理工具Robo 3T

还是在koa2项目下安装mongoose

npm i mongoose

在koa2根目录下新建一个dbs文件夹,在此文件夹下新建配置文件config.js文件,写入

module.exports = {
  // 在本机的数据库下新建了一个数据库dbs
  dbs: 'mongodb://127.0.0.1:27017/dbs'
}

然后在dbs文件夹下新建models文件夹,在此文件夹下新建配置文件person.js文件,写入

const mongoose = require('mongoose')

// 建立一个人的数据表
let personSchema = new mongoose.Schema({
  name: String,
  age: Number
})

//由数据表生成一个Person模型并导出
module.exports = mongoose.model('Person', personSchema)

然后需要在服务器中引入这个模型和配置,在app.js中加入

const mongoose = require('mongoose')
const dbconfig = require('./dbs/config')

// 在路由下方加
mongoose.connect(dbconfig.dbs, {
  useNewUrlParser: true
})

然后打开数据库服务,访问27017正常,在项目根目录下执行 npm run dev访问localhost:3000正常即可

虽然我们定义了模型,但没有数据,所以服务器默认不显示,接下来我们要执行数据的增删改查

在router文件夹下的user.js中引入和使用person模型,写入

const router = require('koa-router')()
const Person = require('./../dbs/models/person')

router.prefix('/users')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

router.post('/addPerson', async function (ctx, next) {
  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: code
  }
})
module.exports = router

在git bash命令行下执行以下命令,以post请求添加一条数据

curl -d 'name=lilei&age=27' http://localhost:3000/users/addPerson

此时命令行会返回 {code: 0} 代表数据添加成功,然后打开Robo 3T 就能看到数据库和数据了

重新理一遍思路

首先数据库需要一个数据表,我们在models文件夹下创建了一个person.js文件,这个文件就是对应的数据表

然后要创建这个表下的一些数据,我们通过mogoose创建一个Schema表,在这个表中描述数据,如{name:String,age: Number}

但Schema表只是一个声明,但我们也需要有增删改查的读写能力,通过建立model作为桥梁,让数据与数据表关联,也与Schema表关联

但model只是一个桥梁,不是一个实例,但我们需要实例化,所以在路由中实例化一个类,通过model的save存储方法和路由接口就把数据写进去了

上面只是写入的方法,接下来创建读取的方法,注意路由需换

router.post('/getPerson', async function (ctx) {
  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
  }
})

然后在git bash命令行下执行以下命令,就可以取到数据

curl -d 'name=lilei' http://localhost:3000/users/getPerson

接下来创建修改的方法

router.post('/updatePerson', async function (ctx) {
  const result = await Person.where({name: ctx.request.body.name}).update({age: ctx.request.body.age})
  ctx.body = {
    code: 0
  }
})

然后在git bash命令行下执行以下命令,就可以修改数据

curl -d 'name=lilei&age=30' http://localhost:3000/users/updatePerson

创建删除的方法

router.post('/removePerson', async function (ctx) {
  const result = await Person.where({name: ctx.request.body.name}).remove()
  ctx.body = {
    code: 0
  }
})

 然后在git bash命令行下执行以下命令,就可以删除数据

curl -d 'name=lilei' http://localhost:3000/users/removePerson
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风里有诗句哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值