我目前正在使用mongoDB,其中面临在子文档中添加元素的一些问题。我做了如下所示的事情。
当我添加新元素时,名称字段保持为空
{
"_id": ObjectId("595f63c45fe41321f7a0e23b"),
"point": 0
},
{
"_id": ObjectId("595f63d45fe41321f7a0e23c"),
"point": 0
}
var playerSchema = new Schema(
{ name : {type:String},
point : {type: Number,default:0}
});
var matchSchema = new Schema(
{
player_one : {type: Schema.Types.ObjectId,required: true,ref:'Player'},
player_two : {type: Schema.Types.ObjectId,required: true,ref:'Player'},
round : {type: Number,required: true,default:0},
winner : {type: Schema.Types.ObjectId},
loser : {type: Schema.Types.ObjectId}
});
var tournamentSchema = new Schema(
{
tournament_name: {type:String ,unique : true},
tour_owner : {type: Schema.Types.ObjectId,required: true,ref:'User'},
players : [playerSchema],
matches : [matchSchema],
});
var Tournament = mongoose.model('Tournament',tournamentSchema);
function addPlayerToTournament(playerName,tourneyName,cb) {
getTourneyByName(tourneyName,tourneyDetails);
function tourneyDetails(tourney) {
tourney.players.push({player:{name:playerName}})
tourney.save(function(err,result) {
if(err)
throw err;
cb(result);
});
}
}
提前致谢......