Node.js更新mongodb数组中的元素

时间:2013-03-17 01:05:18

标签: node.js mongodb

我有以下Mongoose架构和模型:

//GameBoards
var gameBoardSchema = mongoose.Schema({
  mCurrentPlayer: Number,
  mCurrentPlayerName: String,
  mStarted: Boolean,
  mSwitching: Boolean,
  mFinished: Boolean,
  mPlayers: [{mPlayerId: String, mUsername: String, mHand: [{mValue: Number, mSuit:     String}], mFaceDown: [{mValue: Number, mSuit: String}], mFaceUp: [{mValue: Number, mSuit: String}], mPosition: Number, mSwitching: Boolean}],
  mDeck: [{mValue: Number, mSuit: String}],
  mPile: [{mValue: Number, mSuit: String}],
  mStartedAt: {type: Date},
  mLastUpdate: {type: Date},
  mChanceTaken: Boolean,
  mRoundLenght: Number,
  mNumberOfPlayers: Number,
  mLocked: Boolean
});
var GameBoard = mongoose.model('gameBoard', gameBoardSchema);

我的数据库中已经有四个玩家使用了gameBoard。现在我正尝试使用以下命令更新数据库中的特定播放器:

GameBoard.update({_id: game._id, "mPlayers.mUsername": updatedPlayer.mUsername}, {$set: {"mPlayers.$": updatedPlayer}}).exec();

但这似乎没有用,但我没有错误= /。我尝试使用相同的参数进行查找,并且像魅力一样。有人可以指出我做错了吗?

1 个答案:

答案 0 :(得分:0)

我尝试了一些不同的东西,似乎猫鼬不喜欢$。当我在做“mPlayers.1”时,它就像一个魅力:)