添加新元素子文档操作时出现的问题

时间:2017-07-07 11:08:59

标签: mongoose mongoose-schema

我目前正在使用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);

我的API

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);
        });
    }
}

提前致谢......

0 个答案:

没有答案