生活随笔
收集整理的這篇文章主要介紹了
koa --- mongoose连接mongoDB
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Mongoose對MongoDB進行操作
const mongoose
= require('mongoose');
mongoose
.connect('mongodb://localhost/test',{
})
Mongoose中的Schema
const categorySchema
= new mongoose.Schema({name
:String
,description
: String
,createdAt
:{type
: Date
,default: Date
.now
}
});
const Category
= mongoose
.model('Category', categorySchema
);
const category
= new Category({name
: 'test',description
: 'test category'
});
category
.save(err
=>{if(err
){console
.error(err
);return}console
.log('saved');
})
Category
.create({name
:'test',description
: 'test category'
}, (err
, category
)=>{if(err
){console
.error(err
);} else {console
.log(category
)}
});
- 通過find方法,查詢name='test’的結果
Category
.find({name
:'test'
}, (err
, res
) =>{if(err
){console
.error(err
)} else{console
.log(res
);}
});
Category
.remove({name
:'test'
}).then(()=>{
})
Category
.update({name
:'test'
},{name
:'test1',description
: 'test1'
}).thenm(()=>{})
栗子
const mongoose
= require('mongoose');
const timeRangeSchema
= new mongoose.Schema({hour
: {type
: Number
,max
: 24,min
: 8},minute
: {type
: Number
,max
: 59,min
: 0},time
: {type
: Number
,get() {return this.get('hour') * 100 + this.get('minute');}}
});const courseSchema
= new mongoose.Schema({name
: String
,startTime
: timeRangeSchema
,endTime
: timeRangeSchema
})
const Course
= mongoose
.model('Course', courseSchema
);module
.exports
= Course
;
const Course
= require('./model/course');const getCourseList
= async () => {return await Course
.find({}).sort({'startTime.time': 1});
}const getCourseById
= async (id
) => {return await Course
.findById(id
);
}const getCourseByTime
= async (start
, end
, weekday
) => {return await Course
.find({weekday
: weekday
}).where('startTime.time').gte(start
.hour
* 100 + start
.minute
).where('endTime.time').lte(end
.hour
* 100 + end
.minute
);
}
const addCourse
= async (course
) => {const { name
, weekday
, startTime
, endTime
} = course
;const item
= await getCourseByTime(startTime
, endTime
, weekday
);if (item
) {throw new Error('當前時間段已經安排了課程');}return await Course
.create(course
);
}const updateCourse
= async (id
, course
) => {return await Course
.update({_id
: id
}, course
);
}const removeCourse
= async (id
) => {return await Course
.remove({_id
: id
});
}module
.exports
= {getCourseList
,getCourseById
,addCourse
,updateCourse
,removeCourse
}
const mongoose
= require('mongoose');const connect
= async () => {await mongoose
.connect('mongodb://localhost/course', {useNewUrlParser
: true,useUnifiedTopology
: true});
}const close
= async () => {await mongoose
.connection
.close();
}module
.exports
= { connect
, close
}
const koa
= require('koa');
const app
= new koa();
const router
= new require('koa-router')();
const bodyParser
= require('koa-bodyparser');
const {getCourseList
,getCourseById
,addCourse
,updateCourse
,removeCourse
} = require('./db');const {connect
,close
} = require('./conn');const JSON_MIME = 'application/json';router
.get('/course', async ctx
=> {ctx
.type
= JSON_MIME;ctx
.body
= {status
: 0,data
: await getCourseList()}
});router
.get('/course/:id', async ctx
=> {ctx
.type
= JSON_MIME;ctx
.body
= {status
: 0,data
: await getCourseById(ctx
.params
.id
)}
});router
.post('/course', async ctx
=> {ctx
.type
= JSON_MIME;await addCourse(ctx
.body
);ctx
.body
= {status
: 0}
});router
.put('/course/:id', async ctx
=> {await updateCourse(ctx
.params
.id
, ctx
.body
);ctx
.body
= {status
: 0}
});router
.delete('/course/:id', async ctx
=> {await removeCourse(ctx
.params
.id
);ctx
.body
= {status
: 0}
})app
.use(async (ctx
, next
) => {await connect()await next()await close()
})app
.use(bodyParser());
app
.use(router
.routes());
app
.listen(3000, async () => {console
.log('Server is running at http://localhost:3000');
})
總結
以上是生活随笔為你收集整理的koa --- mongoose连接mongoDB的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。