原文摘自个人前端博客,欢迎你们来访问javascript
像我这篇文章所说的基于Node.js + jade + Mongoose 模仿gokk.tv,当时中止开发是由于我深深的感受到当时想错了,应该用两个Schema,而不是一个下面又有数组来存,这样取数据是方便,当时分页至关麻烦,不能使用原生提供的limit方法。java
今天看到一本书上有讲,尝试了一把,记录下来mongodb
咱们实验的场景为一个班级有N多学生,先经过学生ID找到班级名称(是否是被玩腻了?)数组
先来将Schema定义好mongoose
ClazzSchema :ui
var mongoose = require('mongoose')
var ClazzSchema = new mongoose.Schema({ clazzName:String }) //其余方法省略.. } module.exports = ClazzSchema
StudentSchema :this
var mongoose = require('mongoose') var StudentSchema = new mongoose.Schema({ name:String, clazzID : { type : mongoose.Schema.ObjectId, ref : 'Clazz' } }) StudentSchema.statics = { findClazzNameByStudentId:function(studentId, callback){ return this .findOne({_id : studentId}).populate('clazzID') .exec(callback) } //其余方法省略.. } module.exports = StudentSchema
能够看到,主须要将ClazzID设为ref到Clazz,依赖为你创建Model时的名称就能够了,要查询Clzz使用populatespa
下面是Model3d
var mongoose = require('mongoose') var ClazzSchema = require('../schemas/clazzSchema') var Clazz = mongoose.model('Clazz',ClazzSchema) module.exports = Clazz
var mongoose = require('mongoose') var StudentSchema = require('../schemas/studentSchema') var Student = mongoose.model('Student',StudentSchema) module.exports = Student
大同小异,着重看test.js
var mongoose = require('mongoose') var Clazz = require('./models/clazzModel') var Student = require('./models/studentModel') //var db = mongoose.createConnection('localhost', 'gokk') mongoose.connect('mongodb://localhost/test') /*var clazz = new Clazz( { clazzName:'软件2班' } ); clazz.save(function (argument){ console.log('true'); });*/ /*var student = new Student({ name : 'hacke2', clazzID : '542b5fcc49df6e741d8d15f5' }) student.save(function (err){ console.log('true'); })*/ Student.findClazzNameByStudentId('542b600a683d59a80d4ee632', function (err, student){ if(err) console.log(err); console.log(student.clazzID.clazzName); })
以前添加了两班级:软件一班和软件二班
咱们在新增hacke2时将classID设为软件2班的,查新hacke2时自动就会把关键的 Class查询到
{ _id: 542b600a683d59a80d4ee632, name: 'hacke2', clazzID: { _id: 542b5fcc49df6e741d8d15f5, clazzName: '软件2班', __v: 0 }, __v: 0 }
end from http://www.hacke2.cn