ger*_*lol 5 javascript mongoose mongodb
我有两个模式,a Team和a Match.我想用它Team Schema来识别队中的队伍Match Schema.到目前为止,这是我的Team和Match JS文件.我想将Team Schema链接到我的Match Schema,以便我可以简单地识别主队或客队,这样我就可以在Match Schema中存储一个实际的Team对象.
这样我可以参考主队例如Match.Teams.home.name = England(这只是一个例子)
Team.js
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validatePresenceOf = function(value){
return value && value.length;
};
var getId = function(){
return new Date().getTime();
};
/**
* The Team schema. we will use timestamp as the unique key for each team
*/
var Team = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'name' : { type : String,
validate : [validatePresenceOf, 'Team name is required'],
index : { unique : true }
}
});
module.exports = mongoose.model('Team', Team);
Run Code Online (Sandbox Code Playgroud)
这就是我想用Match.js做的事情
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeamSchema = require('mongoose').model('Team');
var validatePresenceOf = function(value){
return value && value.length;
};
var toLower = function(string){
return string.toLowerCase();
};
var getId = function(){
return new Date().getTime();
};
/**
* The Match schema. Use timestamp as the unique key for each Match
*/
var Match = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'hometeam' : TeamSchema,
'awayteam' : TeamSchema
});
module.exports = mongoose.model('Match', Match);
Run Code Online (Sandbox Code Playgroud)
您的解决方案:使用实际架构,而不是使用该架构的模型:
module.exports = mongoose.model('Team', Team);
Run Code Online (Sandbox Code Playgroud)
到
module.exports = {
model: mongoose.model('Team', Team),
schema: Team
};
Run Code Online (Sandbox Code Playgroud)
然后直接var definition = require('path/to/js');使用而definition.schema不是模型
| 归档时间: |
|
| 查看次数: |
7905 次 |
| 最近记录: |