MongoDB多表關(guān)聯(lián)查詢操作實(shí)例詳解
本文實(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)
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)
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)
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)
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的編譯中
StudentSchema.static = { fetch: function(cb){ return this .find({}) .sort('meta.updateAt') //按更新的時(shí)間排序 } }
2. 將靜態(tài)類方法加到Model中
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í))
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)
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ì)有所幫助。
相關(guān)文章
將MongoDB加入到Windows的本地服務(wù)項(xiàng)的方法
下面主要針對(duì)MongoDB在Windows下加入本地服務(wù)項(xiàng)做一些簡(jiǎn)單的分享。以方便剛接觸MongoDB并在Windows環(huán)境下進(jìn)行開發(fā)的同學(xué)2014-08-08MongoDB整庫備份與還原以及單個(gè)collection備份、恢復(fù)方法
mongodb數(shù)據(jù)庫維護(hù)離不開必要的備份、恢復(fù)操作,而且一般不會(huì)出錯(cuò),所以我們?cè)谑褂玫臅r(shí)候大部分時(shí)候使用備份和恢復(fù)操作就可以了2013-08-08Vercel+MongoDB Atlas部署詳細(xì)指南
這篇文章主要為大家介紹了Vercel+MongoDB Atlas部署的詳細(xì)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08MongoDB在系統(tǒng)數(shù)據(jù)庫local中無法創(chuàng)建用戶的解決辦法
這篇文章主要給大家介紹了關(guān)于MongoDB在系統(tǒng)數(shù)據(jù)庫local中無法創(chuàng)建用戶的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11MongoDB數(shù)據(jù)庫設(shè)置賬號(hào)密碼完整步驟
MongoDB這工具很好用的,頁面美觀,設(shè)置賬號(hào)密碼也必不可少,下面這篇文章主要給大家介紹了關(guān)于MongoDB數(shù)據(jù)庫設(shè)置賬號(hào)密碼的完整步驟,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2023-05-05MongoDB學(xué)習(xí)筆記之MapReduce使用示例
這篇文章主要介紹了MongoDB學(xué)習(xí)筆記之MapReduce使用示例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-07-07Mongodb使用$pop刪除數(shù)組中元素的操作指南
本文描述怎樣從Mongodb的文檔數(shù)組字段中,使用$pop刪除數(shù)組中的元素,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06