Arrays $push到MongoDB中带有mongoose的阵列';行不通

Arrays $push到MongoDB中带有mongoose的阵列';行不通,arrays,mongodb,mongoose,types,push,Arrays,Mongodb,Mongoose,Types,Push,errmsg:'字段'weight\'必须是数组,但在中为int类型 文件 我的模式: weight: [{ type: Number }] 以及我的发帖请求: app.post('/edit', function(req, res){ var update = { $push: {"weight": req.body.weight}}; User.findOneAndUpdate(conditions, update, options, function (err)

errmsg:'字段'weight\'必须是数组,但在中为int类型 文件

我的模式:

weight: [{
  type: Number
}]
以及我的发帖请求:

app.post('/edit', function(req, res){
    var update = {  $push: {"weight": req.body.weight}};
    User.findOneAndUpdate(conditions, update, options, function (err)
    {
      if (err)
      {
          console.log(err);
      }
      else
      {
          console.log('yep');
      }
    })
});

如果集合中有多个文档符合您的
条件
,您可以通过将
{weight:{$type:4}}
添加到
条件
中,只更新一个合适的文档


否则,应用程序的架构与数据库中的数据不匹配。

如果集合中有多个文档符合您的
条件
,则只能通过将
{weight:{$type:4}}
添加到
条件
中来更新合适的文档

否则,应用程序的架构与数据库中的数据不匹配。

这可能有效

//模式

weight: [Number]
weight: [{
  weight: {
    type: Number
  }
}]

//如果将对象推入数组,也可以这样做

//模式

weight: [Number]
weight: [{
  weight: {
    type: Number
  }
}]
//然后在API中

var update = {  $push: {"weight": { "weight": req.body.weight }}};
这可能有用

//模式

weight: [Number]
weight: [{
  weight: {
    type: Number
  }
}]

//如果将对象推入数组,也可以这样做

//模式

weight: [Number]
weight: [{
  weight: {
    type: Number
  }
}]
//然后在API中

var update = {  $push: {"weight": { "weight": req.body.weight }}};

我使用一个用户id作为条件,所以我只有一条记录,然后它会进入答案的“其他”部分。我使用一个用户id作为条件,所以我只有一条记录,然后它会进入答案的“其他”部分。看起来您的字段类似于
{“权重”:3}
在db中,您正在使用
$push
将数组值推送到
int
类型字段中。因此,我应该将模式更改为smth,如:weight:[{type:array}]?它不起作用不,架构定义是正确的。我的意思是建议您首先修复
weight
字段中的数据。它在db中看起来应该像
{“weight”:[3]}
,您可以使用
$push
的更新将更多值添加到数组中。因此,您可能需要像更新脚本一样首先更改数据。看看这个答案是否有用。看起来您的字段类似于数据库中的
{“weight”:3}
,您正在使用
$push
将数组值推入
int
类型字段。因此,我应该将模式更改为smth,如:weight:[{type:array}]?它不起作用不,架构定义是正确的。我的意思是建议您首先修复
weight
字段中的数据。它在db中看起来应该像
{“weight”:[3]}
,您可以使用
$push
的更新将更多值添加到数组中。因此,您可能需要像更新脚本一样首先更改数据。看看这个答案是否有用。