简体   繁体   English

如何在 Mongoose 中更新对象内的属性

[英]How can I update a property inside an object in Mongoose

i'm trying to update a property inside an object using $inc in mongoose.我正在尝试在 mongoose 中使用 $inc 更新对象内的属性。 I've tried several ways but apparently the syntax is not valid.我尝试了几种方法,但显然语法无效。

this is the relevant part of the Schema:这是架构的相关部分:

stats: {
    type: {
      totalMatches: {
        type: Number,
        default: 0
      },
      totalWins: {
        type: Number,
        default: 0
      },
      totalRebuys: {
        type: Number,
        default: 0
      },
      totalTimesHosted: {
        type: Number,
        default: 0
      },
      averagePosition: {
        type: Number,
        default: 0
      },
      gainLossRatio: {
        type: Number,
        default: 0
      },
      totalFinishesForEachPosition: {
        type: [Number],
        default: [0]
      }
    }
  }
});

const UserModel = mongoose.model("User", userSchema);

This is the part for the update, the syntax error is inside the $inc block:这是更新的部分,语法错误在 $inc 块内:

UserModel.savePokerResultToPlayerData = (game) => {
  _.each(game.results, (playerResult, index) => {
    let resultToSave = {
      matchId: game._id,
      date: game.date,
      ranking: index + 1,
      prizeMoney: playerResult.prizeMoney,
      rebuys: playerResult.rebuys,
      isHostPlayer: game.host === playerResult._id
    };
    const statsObject = prepareStatsDataToBeUpdated(resultToSave);
    UserModel.findByIdAndUpdate(
      playerResult._id,
      {
        $push: { results: resultToSave },
        $inc: {
          stats.totalMatches: 1,
          stats.totalWins: statsObject.totalWins,
          stats.totalRebuys: statsObject.totalRebuys,
          stats.totalTimesHosted: statsObject.totalTimesHosted
        }
      }
    )
    .exec()
    .then()
    .catch(err => {
      console.log('error: ' + err);
    });
  });
};

prepareStatsDataToBeUpdated = (resultToSave) => {
  return {
    totalWins: resultToSave.ranking === 1 ? 1 : 0,
    totalRebuys: resultToSave.rebuys,
    totalTimesHosted: resultToSave.isHostPlayer ? 1 : 0
  };
};

I've looked at a few similar questions here and tried the solution but all of them got me a syntax error.我在这里查看了一些类似的问题并尝试了解决方案,但所有这些问题都给我带来了语法错误。 I know i can find the related user, work on it and save it but i believe it loses the purpose of $inc and $push.我知道我可以找到相关用户,对其进行处理并保存它,但我相信它失去了 $inc 和 $push 的目的。

Probably you got syntax error about javascript.可能您遇到了有关 javascript 的语法错误。 It's forbidden to write invalid variable name on the left side of the ":" when you define object, here you use dot notation in place where have to be valid variable name or string:定义对象时,禁止在“:”的左边写无效的变量名,这里在必须是有效的变量名或字符串的地方使用点表示法:

stats.totalMatches: 1

please use quotes:请使用引号:

"stats.totalMatches": 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Mongoose 中的数组中删除 object 的属性? !重要的 - How can I remove a property of object from an array in Mongoose? !Important 如何在reducer中更新对象的1个属性? - How can I update 1 property of object in reducer? 如何更新 mongoose 中 createdAt 字段的 expires 属性以避免文档被删除? - How can I update the expires property on createdAt field in mongoose to avoid the document being deleted? 如何更新Mongoose中的文档? - How can I update a document in Mongoose? 如何替换对象内部的属性名称? 使用Javascript - How can I replace property names inside of an object? Javascript 如何通过另一个属性找到猫鼬模型的属性? - How can I find a property of a mongoose model by another property? 我如何在另一个内部引用 1 ZCCADDEDB567ABAE643E15DCF0974E503Z Schema 作为其属性? - How do i reference 1 Mongoose Schema inside another as its property? 如何复制整个 JavaScript object 以更新包含嵌套 arrays 和子文档的 MongoDB/Mongoose 文档? - How can I copy an entire JavaScript object to update a MongoDB/Mongoose document, that contains nested arrays and subdocuments? 如何使用 Redux 更新对象数组中的状态? - How can I update a state inside an array of object with Redux? 如何更新数组内的json对象? - How can I update a json object inside of an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM