饿虎岗资源网 Design By www.oxmxm.com

本文实例讲述了MongoDB多表关联查询操作。分享给大家供大家参考,具体如下:

Mongoose的多表关联查询

首先,我们回忆一下,MySQL多表关联查询的语句:

student表:

MongoDB多表关联查询操作实例详解

calss表:

MongoDB多表关联查询操作实例详解

通过student的classId关联进行查询学生名称,班级的数据:

SELECT student.name,student.age,class.name FROM student,class WHERE student.classId = class.id

Mongoose多表联合查询(还是以众所周知的学生、班级作为实例)

· 表结构的定义(schemas目录下)

1. student表(student.js)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/*定义数据模式*/
var StudentSchema = new mongoose.Schema({
  name: String,
  calssId: {
    type: Schema.Types.objectId,
    ref: 'class'
  },
  age: Number,
  number: Number,
  meta: {
    createAt: {
      type: Date,
      default: Date.now()
    },
    updateAt: {
      type: Date,
      default: Date.now()
    }
  }
  /*更新时间的*/
});
module.exports = StudentSchema;

2. class表(class.js)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/*定义数据模式*/
var ClassSchema = new mongoose.Schema({
  name: String,
  meta: {
    createAt: {
      type: Date,
      default: Date.now()
    },
    updateAt: {
      type: Date,
      default: Date.now()
    }
  }
  /*更新时间的*/
});
module.exports = ClassSchema;

· 生成Model(model目录下)

1. student Model(student.js)

var mongoose = require('mongoose');
var StudentSchema = require('../schemas/student');
/*通过model编译模式为模型*/
var Student = mongoose.model('student', StudentSchema);
/*导出Student模型 模块*/
module.exports = Student;

2. class Model(class.js)

var mongoose = require('mongoose');
var ClassSchema = require('../schemas/class');
/*通过model编译模式为模型*/
var Class = mongoose.model('class', ClassSchema);
/*导出Class模型 模块*/
module.exports = Class;

· Model进行数据的查询操作

1. 将静态类的方法加到Model的编译中

StudentSchema.static = {
  fetch: function(cb){
 return this
   .find({})
   .sort('meta.updateAt') //按更新的时间排序
  }
}

2. 将静态类方法加到Model中

StudentSchema.static('fetch', function(cb){
   return this
     .find({}, cb)
  .sort('meta.updateAt')
})

3. 直接调用model的find()方法

查询的结果均为:

[
    {
        _id: '5a05222f583e5720b8660191',
        name: '张三',
        age: 18,
        number: 11,
        classId: '5a0036512b740f32e4371e66'
    },
    {
        _id: '5a05222f583e5720b8660091',
        name: '李四',
        age: 19,
        number: 11,
        classId: '5a0036512b740f32e1371e66'
    },
    {
        _id: '5a05222f583e5720b18660191',
        name: '赵五',
        age: 17,
        number: 11,
        classId: '5a0036512b7420f32e4371e66'
    }
]

· 多表联合查询(学生对应班级)

StudentSchema.static = {
  findStudentWithClass: function (cb) {
    return this
      .find({})
      .populate('classId')//注意这是联合查询的关键
      .sort('meta.updateAt')
      .exec(cb)
  }
}

查询结果:

[
    {
        _id: '5a05222f583e5720b8660191',
        name: '张三',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b740f32e4371e66',
            name: '一年1班'
        }
    },
    {
        _id: '5a05222f583e5720b8660091',
        name: '李四',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b740f32e1371e66',
            name: '二年2班'
        }
    },
    {
        _id: '5a05222f583e5720b18660191',
        name: '赵五',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b7420f32e4371e66',
            name: '一年2班'
        }
    }
]

· 由上面的实例可知,mongoose的多表联合查询的关键:

1. 数据模式结构定义需要利用关键字ref定义关联

var Schema = new mongoose.Schema({
  field: {
 type: mongoose.Schema.Type.ObjectId,
 ref: 'model'
  }
});
Schema.static = {
  fetch: function(cb){
 return this
    .find({})
    .populate('field')
    .exec(cb)
 }
 }
var Model = mongoose.Model('model',Schema );

希望本文所述对大家MongoDB数据库程序设计有所帮助。

饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。

更新日志

2024年09月21日