首页
Javascript
Html
Css
Node.js
Electron
移动开发
小程序
工具类
服务端
浏览器相关
前端收藏
其他
关于
公司注册

mongodb之多表关联查询并分页

2020年02月29日 发布 阅读(5054) 作者:Jerman

Model

多表查询分页可以使用mongoose-aggregate-paginate-v2

interface表Model

  1. const aggregatePaginate = require('mongoose-aggregate-paginate-v2');
  2. module.exports = app => {
  3. const mongoose = app.mongoose;
  4. const schema = new mongoose.Schema({
  5. // 自增ID
  6. id: { type: Number, unique: true },
  7. // ...省略
  8. // 创建人
  9. createBy: { type: String },
  10. // 更新时间
  11. updateTime: { type: String },
  12. // 更新人
  13. updateBy: { type: String },
  14. });
  15. schema.plugin(aggregatePaginate);
  16. // mongoose.model(名称,数据,表名)
  17. return mongoose.model('system Interface', schema, 'interface');
  18. };

user表Model

  1. const aggregatePaginate = require('mongoose-aggregate-paginate-v2');
  2. module.exports = app => {
  3. const mongoose = app.mongoose;
  4. const schema = new mongoose.Schema({
  5. // 自增ID
  6. id: { type: Number, unique: true },
  7. // 用户唯一ID
  8. uuid: { type: String, unique: true },
  9. // 用户角色
  10. roleId: { type: Number },
  11. // 用户名-用于登录,找回密码
  12. userName: { type: String, unique: true },
  13. // ...省略
  14. });
  15. schema.plugin(aggregatePaginate);
  16. // mongoose.model(名称,数据,表名)
  17. return mongoose.model('user-list', schema, 'user');
  18. };

Service

命令功能描述
$project指定输出文档里的字段.
$match选择要处理的文档,与fine()类似。
$limit限制传递给下一步的文档数量。
$skip跳过一定数量的文档。
$unwind扩展数组,为每个数组入口生成一个输出文档。
$group根据key来分组文档。
$sort排序文档。
$geoNear选择某个地理位置附近的的文档。
$out把管道的结果写入某个集合。
$redact控制特定数据的访问。
$lookup多表关联(3.2版本新增)

这里就是使用$lookup进行多表关联查询,$lookup可以设置多个,通过不同的字段,关联不同的表

  1. async list(page, pageSize, query, select) {
  2. const { ctx } = this;
  3. // 关联查询user表
  4. const aggregateQuery = ctx.model.System.Interface.aggregate([
  5. {
  6. $lookup: {
  7. // 关联的表
  8. from: 'user',
  9. // 当前表关联的字段(如现在的Interface表)
  10. localField: 'createBy',
  11. // 关联的表的字段(user表的userName字段)
  12. foreignField: 'userName',
  13. // 查询出来的关联数据,存放在'createUser'字段中
  14. as: 'createUser',
  15. },
  16. },
  17. {
  18. $lookup: {
  19. from: 'user',
  20. localField: 'updateBy',
  21. foreignField: 'userName',
  22. as: 'updateUser',
  23. },
  24. },
  25. {
  26. // $project:设置0隐藏某些字段 ,让其不返回
  27. $project: {
  28. createUser: {
  29. _id: 0,
  30. __v: 0,
  31. uuid: 0,
  32. password: 0,
  33. updateBy: 0,
  34. updateTime: 0,
  35. createTime: 0,
  36. createBy: 0,
  37. registerTime: 0,
  38. },
  39. updateUser: {
  40. _id: 0,
  41. __v: 0,
  42. uuid: 0,
  43. password: 0,
  44. updateBy: 0,
  45. updateTime: 0,
  46. createTime: 0,
  47. createBy: 0,
  48. registerTime: 0,
  49. },
  50. __v: 0,
  51. ...select,
  52. },
  53. },
  54. ]);
  55. const list = await ctx.model.System.Interface.aggregatePaginate(
  56. aggregateQuery,
  57. {
  58. page: Number(page) || 1,
  59. // 每页数
  60. limit: Number(pageSize) || 10,
  61. sort: { menuId: 1 },
  62. },
  63. );
  64. return list;
  65. }
版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处