MongoDB多表关联查询操作实例详解
本文实例讲述了mongodb多表关联查询操作。分享给大家供大家参考,具体如下:
mongoose的多表关联查询
首先,我们回忆一下,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: '张三',
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数据库程序设计有所帮助。
下一篇: Vue form表单动态添加组件实战案例