首页 > 解决方案 > 推入数组的 MongoDB(猫鼬)忽略了属性

问题描述

我有一个看起来像这样的用户模式:

reputation: [
{
  voter_id: {
    type: String,
    required: true,
  },
},
{
  score: {
    type: Number,
    required: true,
  },
},
{
  comment: {
    type: String,
    maxlength: 100,
  },
},
{
  voted_at: {
    type: Date,
    default: Date.now,
  },
},
],

每当我尝试像这样将一个对象推入这个数组时

User.updateOne(
  { id: user.id },
  {
    $push: {
      reputation: {
        voter_id: interaction.user.id,
        score: rating,
        comment,
      },
    },
  },
);

只有要推送的对象的第一个属性,这里的voter_id是在数据库中设置的,即使其他值不是未定义的。

存储在数据库中的对象如下所示:

{ "_id" : ObjectId("61478043630ef626f50c9c2b"), "reputation" : [ { "voter_id" : "214772155114717184", "_id" : ObjectId("61478060630ef626f50c9c7c") } ], "__v" : 0 }

属性缺失。对于此示例,分数为 1 并注释“测试”。这里有什么问题?

标签: javascriptnode.jsdatabasemongodbmongoose

解决方案


推荐阅读