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

  • 时间:
  • 浏览:36

本文真例报告了MongoDB多表联系关系查询操纵。分享给各人供各人参考,详细以下:

Mongoose的多表联系关系查询

起首,我们回想1下,MySQL多表联系关系查询的语句:

student表:

calss表:

经由过程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: '张3',
        age: 18,
        number: 11,
        classId: '5a0036512b740f32e4371e66'
    },
    {
        _id: '5a05222f583e5720b8660091',
        name: '李4',
        age: 19,
        number: 11,
        classId: '5a0036512b740f32e1371e66'
    },
    {
        _id: '5a05222f583e5720b18660191',
        name: '赵5',
        age: 17,
        number: 11,
        classId: '5a0036512b7420f32e4371e66'
    }
]

· 多表结合查询(教死对应班级)

StudentSchema.static = {
  findStudentWithClass: function (cb) {
    return this
      .find({})
      .populate('classId')//留意那是结合查询的枢纽
      .sort('meta.updateAt')
      .exec(cb)
  }
}

查询成果:

[
    {
        _id: '5a05222f583e5720b8660191',
        name: '张3',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b740f32e4371e66',
            name: '1年1班'
        }
    },
    {
        _id: '5a05222f583e5720b8660091',
        name: '李4',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b740f32e1371e66',
            name: '2年2班'
        }
    },
    {
        _id: '5a05222f583e5720b18660191',
        name: '赵5',
        age: 18,
        number: 11,
        classId: {
            _id: '5a0036512b7420f32e4371e66',
            name: '1年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数据库法式设想有所帮忙。