我有两个模式,Team
和Match
。我想使用Team Schema
来标识Match Schema
中的团队。到目前为止,这是我的Team and Match JS文件。我想将团队架构链接到我的比赛架构,以便我可以简单地识别主队或客队,以便在比赛架构中存储一个实际的团队对象。
这样,我可以将主队称为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);
这就是我想用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);
最佳答案
您的解决方案:使用实际模式,而不是使用模式的模型:
module.exports = mongoose.model('Team', Team);
至
module.exports = {
model: mongoose.model('Team', Team),
schema: Team
};
然后使用
var definition = require('path/to/js');
,并直接使用definition.schema
而不是模型