国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MongoDB > 正文

MongoDB多表關聯查詢操作實例詳解

2020-10-29 18:41:37
字體:
來源:轉載
供稿:網友

本文實例講述了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數據庫程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连云港市| 正安县| 南雄市| 沂源县| 安顺市| 布尔津县| 墨竹工卡县| 句容市| 天等县| 南开区| 桦南县| 西畴县| 察隅县| 弥渡县| 宁陕县| 凌云县| 沂水县| 洪洞县| 绿春县| 华池县| 仙游县| 嵩明县| 双辽市| 云龙县| 安多县| 乐至县| 布尔津县| 昌平区| 左权县| 永新县| 平远县| 剑阁县| 霍林郭勒市| 普格县| 石狮市| 楚雄市| 临高县| 商河县| 新民市| 同江市| 紫阳县|