使用mongoose将字段设置为字符串

时间:2013-12-12 02:18:39

标签: mongoose

有没有比使用JSON.parse更好(更好)的方法?

userSchema.methods.incrementPlaylist = function(playlist, value) {
  return this.update(JSON.parse("{\"$inc\": {\"playlists." + playlist + "\": " + value + "}}"));
};

1 个答案:

答案 0 :(得分:0)

是的,请改为建立您的更新对象:

userSchema.methods.incrementPlaylist = function(playlist, value) {
  var update = { $inc: {} };
  update.$inc['playlists.' + playlist] = value;
  return this.update(update);
};