我有两个模式,一个Team和一个Match。我想使用Team Schema来识别Match Schema中的团队。到目前为止,这是我的团队和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);下面是我想要对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);发布于 2013-02-06 21:52:14
您的解决方案:使用实际的模式,而不是使用该模式的模型:
module.exports = mongoose.model('Team', Team);至
module.exports = {
model: mongoose.model('Team', Team),
schema: Team
};然后使用var definition = require('path/to/js');,并直接使用definition.schema而不是模型
发布于 2014-05-01 06:09:04
您不希望嵌套模式。
在Mongoose中尝试Population:http://mongoosejs.com/docs/populate.html,它将解决您的问题。
发布于 2020-05-24 16:35:27
尝试在Match.js中使用Schema.Types.ObjectId:
hometeam: { type: Schema.Types.ObjectId, ref: 'Team' } awayteam: { type: Schema.Types.ObjectId, ref: 'Team' }
https://stackoverflow.com/questions/14730676
复制相似问题