一、获取表长度
// 获取用户总数量
router.get('/getArticleNum', new Auth().tokenVerify, async ctx => {
const userNum = await modelUser.find()
const res = userNum.length
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
二、增加一条数据
router.post('/add_user', async ctx => {
const params = ctx.request.body
const res = await modelUser({
mobile: params.mobile,
password: params.password,
nickname: params.nickname,
gender: params.gender,
city: params.city, uid: params.uid,
avatarUrl: params.avatarUrl,
backdrop: params.backdrop,
my_tags: params.my_tags
}).save()
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
三、删除一条数据
router.post('/article_del', new Auth().tokenVerify, async ctx => {
const { _id } = ctx.request.body//评论_id
const res = await modelArticle.findOneAndDelete({ _id: _id })
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
四、修改数据
// 修改文章
router.post('/article_update', new Auth().tokenVerify, async ctx => {
const { _id } = ctx.query
const massage = ctx.request.body
const res = await modelArticle.findOneAndUpdate({ _id: _id }, {
title: massage.title,
content: massage.content,
cover_image: massage.cover_image,
city: massage.city,
tag: massage.tag,
time: massage.time
})
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
五、按照某一字段查询
// 按城市分类查询
router.post('/article_info_city', new Auth().tokenVerify, async ctx => {
const { city } = ctx.request.body
const res = await modelArticle.find({ city: city })
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
六、获取某一个字段数量排名
// 获取游记前五的城市
router.get('/getCity', new Auth().tokenVerify, async ctx => {
const cityRanking = await modelArticle.aggregate([{ $group: { _id: '$city', count: { $sum: 1 } } }, { $sort: { count: -1 } }])
const res = cityRanking
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})
七、获取某一字段的值占比
// 获取用户男女占比
router.get('/getGender', new Auth().tokenVerify, async ctx => {
// const { gender } = ctx.request.body//评论_id
const male = await modelUser.find({ gender: '男' })
const maleNum = male.length
const female = await modelUser.find({ gender: '女' })
const femaleNum = female.length
const unkonwn = await modelUser.find({ gender: '' })
const unkonwnNum = unkonwn.length
const res = [{ '男': maleNum }, { '女': femaleNum }, { '未知': unkonwnNum }]
new Interfaceresult(ctx, 'SUCCESS', 200, res).answer()
})