Reputation: 343
I have a Schema called Players and another one called Game. Every Games has an attribute called players which is an array of references to player objects.
Game Schema
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 Connection
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/ScoreKeeper", { useMongoClient: true });
Code for initializing Games and Players on the post route. (I am using 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");
}
});
}
});
}
}
});
});
On Terminal on the node console I have the following output, when I create 5 players:
New Player added
New Player added
New Player added
New Player added
New Player added
Now when I go to the mongo console and check out the players collection, I find there are 5 players created as expected. Using "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 }
However when I check the game object in the mongo console, I find this ...
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
}
There is a mismatch between Games and Players, I feel that the bug is in saving the newGame
after adding newPlayer
. Strangely enough _v
property reads 5 as expected. Could you please point me towards fixing the bug?
Thank-you
Upvotes: 0
Views: 1373
Reputation: 12817
There is a bug in saving the game document in your code
Instead of save
use update
method with push
to add ObjectId
s of players
newGame.update({$push:{players : newPlayer._id} }, function(error, newGame) {...}
console out
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
}
>
Upvotes: 1