【发布时间】:2015-11-14 18:33:10
【问题描述】:
当玩家加入游戏时,我正在使用 mongoose 将玩家推入 mongoose 中的玩家数组。我收到以下错误:
"Can't canonicalize query: BadValue unknown top level operator: $push"
我如何格式化 $push 似乎有问题,但是 我试图遵循这个答案,但不确定我的代码与它们的实现有何不同: pushing object into array schema in Mongoose
我有以下架构:
var GameSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.Mixed,
players: [{
username: String,
currentRound: Number
}],
// the number represents the qNumber in the questionData.js
// and also the round number for what the player is currently on
questions: [Number]
});
以及下面的更新调用
var Game = mongoose.model('Game', GameSchema);
var updateGame = function(req,res,next){
Game.findByIdAndUpdate(
req.body.code,
{$push: {"players": {username: "hardcode", currentRound: 1}}},
{safe: true, upsert: true, new: true},
function(err, model){
if (err){
console.log("ERROR: ", err);
res.send(500, err);
}else{
res.status(200).send(model);
}
}
);
};
【问题讨论】: