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

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

MongoDB多表關(guān)聯(lián)查詢的語句是什么?如何操作?

2024-09-07 00:22:30
字體:
供稿:網(wǎng)友
       我們在使用MongoDB時,經(jīng)常需要進(jìn)行MongoDB多表關(guān)聯(lián)查詢,因此多表關(guān)聯(lián)查詢也是需要掌握的內(nèi)容,本文就給大家分享一下MongoDB多表關(guān)聯(lián)查詢實例,有需要的朋友可以看看。
 
       Mongoose的多表關(guān)聯(lián)查詢
 
       首先,我們回憶一下,MySQL多表關(guān)聯(lián)查詢的語句:
 
 
       通過student的classId關(guān)聯(lián)進(jìn)行查詢學(xué)生名稱,班級的數(shù)據(jù):SELECT student.name,student.age,class.name FROM student,class WHERE student.classId = class.id
 
       Mongoose多表聯(lián)合查詢(還是以眾所周知的學(xué)生、班級作為實例)
 
       ・ 表結(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()
    }
  }
  /*更新時間的*/
});
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()
    }
  }
  /*更新時間的*/
});
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') //按更新的時間排序
  }
}
 
       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é)生對應(yīng)班級)
 
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班'
        }
    }
]
 
       ・ 由上面的實例可知,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 ); 

(編輯:武林網(wǎng))

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 香格里拉县| 芮城县| 临汾市| 祥云县| 腾冲县| 通许县| 盈江县| 梅河口市| 漾濞| 江永县| 海盐县| 新津县| 襄垣县| 庄河市| 乐陵市| 固安县| 梁山县| 图们市| 太和县| 准格尔旗| 遵化市| 迭部县| 宁阳县| 彭山县| 闽清县| 理塘县| 怀来县| 肇庆市| 宣化县| 左权县| 东光县| 乌鲁木齐县| 新安县| 涟水县| 竹溪县| 漾濞| 称多县| 长汀县| 饶平县| 宝清县| 鲁山县|