我有PlayerModel,看起来像(使用mongoose / mongo)
var PlayerModel = new Schema({
playerUUID : String,
score : Number
});
我想添加20
分,以playerUUID "1261236237621"
分数。
如何查找和更新PlayerModel?可以在一个语句中以sql更新吗?
答案 0 :(得分:1)
首先使用您想要的PlayerModel
找到playerUUID
,然后更新它,例如:
var PlayerModel = new Schema({
playerUUID : String,
score : Number
});
var Player = mongoose.model('Player', PlayerModel);
Player.findOne({ playerUUID: "1261236237621" }, function (err, player) {
if (err) throw new Error(err);
player.score += 20;
player.save(function (err, playerResponse) {
if (err) throw new Error(err);
// playerResponse is your updated Player
console.log(playerResponse);
});
});