const {User} = require('./model')
!(async () =>{
// const updateResult = await User.findOneAndUpdate(
// {username:'zhangsan'},
// {age:25},
// {
// new:true
// }
// )
// console.log('更新的返回结果',updateResult);
const removeResult = await User.removeResult(({username:'list'}))
console.log('删除返回结果',removeResult);
})()
//数据模型(规范数据格式)
const mongoose = require('./db')
//定义schema (数据规范)
const UserSchema = mongoose.Schema({
username:{
type:String,
required:true,
unique:true
},
password:String,
age:Number,
city:String,
//性别
gender:{
type:Number,
default:0
}
},{
timestamps:true //
})
//定义model
const User = mongoose.model('user',UserSchema)
module.exports = {
User
}
const mongoose = require('mongoose')
const url = 'mongodb://localhost:27017'
const dbName = 'comment2'
mongoose.connect(`${url}/${dbName}`,{
useNewUrlParser:true,
useUnifiedTopology:true
})
const conn = mongoose.connection
conn.on('error',err =>{
console.error('mongoose 连接出错',err);
})
module.exports = mongoose //commonjs
{
"name": "test4",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "node bin/www",
"dev": "./node_modules/.bin/nodemon bin/www",
"prd": "pm2 start bin/www",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"debug": "^4.1.1",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-convert": "^1.2.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.0",
"koa-onerror": "^4.1.0",
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"koa-views": "^6.2.0",
"mongoose": "^7.0.1",
"pug": "^2.0.3"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星