本文實(shí)例講述了MongoDB多表關(guān)聯(lián)查詢操作。分享給大家供大家參考,具體如下:
Mongoose的多表關(guān)聯(lián)查詢
首先,我們回憶一下,MySQL多表關(guān)聯(lián)查詢的語句:
student表:
calss表:
通過student的classId關(guān)聯(lián)進(jìn)行查詢學(xué)生名稱,班級(jí)的數(shù)據(jù):
SELECT student.name,student.age,class.name FROM student,class WHERE student.classId = class.id
Mongoose多表聯(lián)合查詢(還是以眾所周知的學(xué)生、班級(jí)作為實(shí)例)
· 表結(jié)構(gòu)的定義(schemas目錄下)
1. student表(student.js)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
var mongoose = require( 'mongoose' ); var Schema = mongoose.Schema; /*定義數(shù)據(jù)模式*/ 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() } } /*更新時(shí)間的*/ }); module.exports = StudentSchema; |
2. class表(class.js)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
var mongoose = require( 'mongoose' ); var Schema = mongoose.Schema; /*定義數(shù)據(jù)模式*/ var ClassSchema = new mongoose.Schema({ name: String, meta: { createAt: { type: Date, default : Date.now() }, updateAt: { type: Date, default : Date.now() } } /*更新時(shí)間的*/ }); module.exports = ClassSchema; |
· 生成Model(model目錄下)
1. student Model(student.js)
1
2
3
4
5
6
|
var mongoose = require( 'mongoose' ); var StudentSchema = require( '../schemas/student' ); /*通過model編譯模式為模型*/ var Student = mongoose.model( 'student' , StudentSchema); /*導(dǎo)出Student模型 模塊*/ module.exports = Student; |
2. class Model(class.js)
1
2
3
4
5
6
|
var mongoose = require( 'mongoose' ); var ClassSchema = require( '../schemas/class' ); /*通過model編譯模式為模型*/ var Class = mongoose.model( 'class' , ClassSchema); /*導(dǎo)出Class模型 模塊*/ module.exports = Class; |
· Model進(jìn)行數(shù)據(jù)的查詢操作
1. 將靜態(tài)類的方法加到Model的編譯中
1
2
3
4
5
6
7
|
StudentSchema.static = { fetch: function (cb){ return this .find({}) .sort( 'meta.updateAt' ) //按更新的時(shí)間排序 } } |
2. 將靜態(tài)類方法加到Model中
1
2
3
4
5
|
StudentSchema.static( 'fetch' , function (cb){ return this .find({}, cb) .sort( 'meta.updateAt' ) }) |
3. 直接調(diào)用model的find()方法
查詢的結(jié)果均為:
[
{
_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'
}
]
· 多表聯(lián)合查詢(學(xué)生對(duì)應(yīng)班級(jí))
1
2
3
4
5
6
7
8
9
|
StudentSchema.static = { findStudentWithClass: function (cb) { return this .find({}) .populate( 'classId' ) //注意這是聯(lián)合查詢的關(guān)鍵 .sort( 'meta.updateAt' ) .exec(cb) } } |
查詢結(jié)果:
[
{
_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班'
}
}
]
· 由上面的實(shí)例可知,mongoose的多表聯(lián)合查詢的關(guān)鍵:
1. 數(shù)據(jù)模式結(jié)構(gòu)定義需要利用關(guān)鍵字ref定義關(guān)聯(lián)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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 ); |
希望本文所述對(duì)大家MongoDB數(shù)據(jù)庫程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/WaterSprite_ct/article/details/78500997