列表的表模型是这样的:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const SchoolSchema = new Schema({
imgUrl: {
type: String,
required: true
},
name: {
type: String,
required: true
},
desc: {
type: String,
required: true
},
isDingdian: {
type: String,
required: true
},
isShow: {
type: String,
default: '1'
},
date: {
type: Date,
default: Date.now
}
})
module.exports = School = mongoose.model("School", SchoolSchema)
详情的表模型是这样的:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const SchoolDetailSchema = new Schema({
schoolId: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
imgUrl: {
type: String,
required: true
},
detail: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = SchoolDetail = mongoose.model("SchoolDetail", SchoolDetailSchema)
查询指定机构的简介和详情,两个表关联 (简介 --- 详情)
var express = require('express');
var router = express.Router();
const School = require("../models/School");
const mongoose = require("mongoose");
//查询指定机构的简介和详情,两个表关联 (简介 --- 详情)
router.post('/school/:id', (req, res) => {
School.aggregate([{//聚合查询
$lookup: { //关联
from: "schooldetails",//关联的表名
localField: "_id", //本身的外键
foreignField: "schoolId",//需要关联表的外键
as: "detail" //起个名字,随便,和下面对应
}
}, {
$unwind: { //这个必须写,不知道啥意思,忘了
path: "$detail", //和上面对应
preserveNullAndEmptyArrays: true //固定的
}
}, {
$match: { //筛选条件
_id: mongoose.Types.ObjectId(req.params.id) //必须加mongoose.Types.ObjectId()
}
}, {
$group: { //组包
_id: "$_id",
imgUrl: {
$first: "$imgUrl"
},
name: {
$first: "$name"
},
desc: {
$first: "$desc"
},
isDingdian: {
$first: "$isDingdian"
},
detail: { //detail是上面起的别名
$first: "$detail" //和上面对应
}
}
}], (err, data) => {
res.json({
code: 200,
data: data,
msg: "查询成功"
})
})
});
最后查出来的数据是这样的
{
"code": 200,
"data": [
{
"_id": "5e97e5321a8a5b870e99f045",
"imgUrl": "http://www.hejicn.com/uploadpic/ueditor/image/20190403/1554268837236717.png",
"name": "上海沐月信息技术发展有限公司",
"desc": "上海沐月信息技术发展有限公司,是一家以特殊儿童康复学习训练为主要服务项目的儿童健康发展中心。",
"isDingdian": "1",
"detail": {
"_id": "5e97eb168c6a9e897365c493",
"schoolId": "5e97e5321a8a5b870e99f045",
"imgUrl": "http://www.hejicn.com/uploadpic/ueditor/image/20190403/1554268837236717.png",
"detail": "我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情我是详情",
"date": "2020-04-16T05:20:22.071Z",
"__v": 0
}
}
],
"msg": "查询成功"
单表的增删改查,删是做的软删除
router.post('/school/add', (req, res) => {
const school = {}
if (req.body.imgUrl) school.imgUrl = req.body.imgUrl;
if (req.body.name) school.name = req.body.name;
if (req.body.desc) school.desc = req.body.desc;
if (req.body.isDingdian) school.isDingdian = req.body.isDingdian;
new School(school).save().then((data) => {
res.json({
code: 200,
data: data,
msg: "添加成功"
});
})
});
//查询机构列表
router.post('/school', (req, res) => {
School.find({
isShow: '1'
}).then((school) => {
if (school == "") {
return res.json({
code: -1,
data: {},
msg: "没有任何信息"
});
} else {
res.json({
code: 200,
data: school,
msg: "获取成功"
})
}
}).catch(err => res.status(400).json(err))
});
router.post("/school/edit/:id", (req, res) => {
const schools = {}
if (req.body.imgUrl) schools.imgUrl = req.body.imgUrl;
if (req.body.name) schools.name = req.body.name;
if (req.body.desc) schools.desc = req.body.desc;
if (req.body.isDingdian) schools.isDingdian = req.body.isDingdian;
School.updateOne({
_id: req.params.id
}, {
$set: schools
}, {
new: true
}).then(data => res.json({
code: 200,
msg: "修改成功"
}))
})
router.post("/school/delete/:id", (req, res) => {
const schools = {}
if (req.body.isShow) schools.isShow = req.body.isShow;
School.updateOne({
_id: req.params.id
}, {
$set: schools
}, {
new: true
}).then(data => res.json({
code: 200,
msg: "删除成功"
}))
})
网友评论