MongoDB多表关联查询的语句是什么?咋样操作?
发布时间:2022-04-22 10:40:51 所属栏目:系统 来源:互联网
导读:我们在使用MongoDB时,经常需要进行MongoDB多表关联查询,因此多表关联查询也是需要掌握的内容,本文就给大家分享一下MongoDB多表关联查询实例,有需要的朋友可以看看。 表结构的定义(schemas目录下) 1. student表(student.js) var mongoose = require(mongo
我们在使用MongoDB时,经常需要进行MongoDB多表关联查询,因此多表关联查询也是需要掌握的内容,本文就给大家分享一下MongoDB多表关联查询实例,有需要的朋友可以看看。 ・ 表结构的定义(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; (编辑:常州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读