添加到父对象的Mongoose子对象未正确保存

时间:2018-01-19 02:41:48

标签: javascript mongodb mongoose

我有一个叫做玩家的架构,另一个叫做游戏。每个游戏都有一个名为players的属性,它是对玩家对象的引用数组。

游戏架构

let GameSchema = new mongoose.Schema({
    players: [{
        ref: "Player",
        type: mongoose.Schema.Types.ObjectId,
    }],
    created: {
        type: Date,
        default: Date.now,
    }
}, { usePushEach: true });

PlayerSchema

let PlayerSchema = new mongoose.Schema({
    parentGameId: mongoose.Schema.Types.ObjectId,
    idInGame: Number,
    points: Number,
});

MongoDb连接

mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/ScoreKeeper", { useMongoClient: true });

在邮政路线上初始化游戏和玩家的代码。 (我正在使用Express)

app.post('/', function(req, res) {
    let players = Number(req.body.players);
    Game.create({
        players: [],
    }, function(error, newGame) {
        if (error) {
            console.log(error);
        } else {
            let currentGameID = newGame._id;
            for (let i = 1; i <= players; i++) {
                Player.create({
                    parentGameId: currentGameID,
                    idInGame: i,
                    points: 0
                }, function(error, newPlayer) {
                    if (error) {
                        console.log(error);
                    } else {  
                        newGame.players.push(newPlayer._id);
                        newGame.save(function(error, newGame) {
                            if (error) {
                                console.log(error);
                            } else {
                                console.log("New Player added");
                            }
                        });

                    }
                });
            }
        }

    });
});

在节点控制台上的终端上,当我创建5个播放器时,我有以下输出:

  

新玩家添加

     

新玩家添加

     

新玩家添加

     

新玩家添加

     

新玩家添加

现在,当我进入mongo控制台并查看球员收藏时,我发现有5名球员按预期创建。使用“db.players.find()”

  

{“_ id”:ObjectId(“5a6159685d3dabb00d0065b0”),“parentGameId”:ObjectId(“5a6159685d3dabb00d0065af”),“idInGame”:1,“points”:0,“__ v”:0}

     

{“_ id”:ObjectId(“5a6159685d3dabb00d0065b1”),“parentGameId”:ObjectId(“5a6159685d3dabb00d0065af”),“idInGame”:2,“points”:0,“__ v”:0}

     

{“_ id”:ObjectId(“5a6159685d3dabb00d0065b2”),“parentGameId”:ObjectId(“5a6159685d3dabb00d0065af”),“idInGame”:3,“points”:0,“__ v”:0}

     

{“_ id”:ObjectId(“5a6159685d3dabb00d0065b3”),“parentGameId”:ObjectId(“5a6159685d3dabb00d0065af”),“idInGame”:4,“points”:0,“__ v”:0}

     

{“_ id”:ObjectId(“5a6159685d3dabb00d0065b4”),“parentGameId”:ObjectId(“5a6159685d3dabb00d0065af”),“idInGame”:5,“points”:0,“__ v”:0}

然而,当我在mongo控制台中检查游戏对象时,我发现了......

  

db.games.findOne()

     

{

"_id" : ObjectId("5a6159685d3dabb00d0065af"),

"created" : ISODate("2018-01-19T02:35:20.632Z"),

"players" : [

    ObjectId("5a6159685d3dabb00d0065b1"),

    ObjectId("5a6159685d3dabb00d0065b1"),

    ObjectId("5a6159685d3dabb00d0065b0"),

    ObjectId("5a6159685d3dabb00d0065b1"),

    ObjectId("5a6159685d3dabb00d0065b0"),

    ObjectId("5a6159685d3dabb00d0065b2"),

    ObjectId("5a6159685d3dabb00d0065b3"),

    ObjectId("5a6159685d3dabb00d0065b3"),

    ObjectId("5a6159685d3dabb00d0065b4")

],

"__v" : 5
     

}

游戏和玩家之间存在不匹配,我觉得错误是在添加newGame后保存newPlayer。奇怪的是,_v属性按预期读取5。你能指点我修复这个错误吗?

感谢您

1 个答案:

答案 0 :(得分:1)

在代码中保存游戏文档存在错误

而不是save使用update方法push来添加ObjectId个玩家

newGame.update({$push:{players : newPlayer._id} }, function(error, newGame) {...}

控制台

Mongoose: games.insert({ _id: ObjectId("5a6168c12478110e7aa74d5d"), created: new Date("Fri, 19 Jan 2018 03:40:49 GMT"), players: [], __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 1, points: 0, _id: ObjectId("5a6168c12478110e7aa74d5e"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 2, points: 0, _id: ObjectId("5a6168c12478110e7aa74d5f"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 3, points: 0, _id: ObjectId("5a6168c12478110e7aa74d60"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 4, points: 0, _id: ObjectId("5a6168c12478110e7aa74d61"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 5, points: 0, _id: ObjectId("5a6168c12478110e7aa74d62"), __v: 0 })
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d62") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d5f") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d61") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d60") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d5e") } }, {})

mongo CLI

> db.players.find()
{ "_id" : ObjectId("5a6168c12478110e7aa74d5f"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 2, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d61"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 4, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d5e"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 1, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d60"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 3, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d62"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 5, "points" : 0, "__v" : 0 }
> 
> 
> db.games.find().pretty()
{
    "_id" : ObjectId("5a6168c12478110e7aa74d5d"),
    "created" : ISODate("2018-01-19T03:40:49.028Z"),
    "players" : [
        ObjectId("5a6168c12478110e7aa74d62"),
        ObjectId("5a6168c12478110e7aa74d5f"),
        ObjectId("5a6168c12478110e7aa74d61"),
        ObjectId("5a6168c12478110e7aa74d60"),
        ObjectId("5a6168c12478110e7aa74d5e")
    ],
    "__v" : 0
}
> 
相关问题