没有删除代码的数组中的猫鼬删除元素

时间:2020-04-20 23:55:37

标签: node.js mongodb mongoose

当我通过mongoose向文档中的特定数组发送许多推送请求时,然后通过解析应用程序中的json显示此数组,该数组中的许多对象被删除或交换而没有任何删除代码。

我尝试了更多时间,但是没有找到解决方法

这是我的代码

exports.push = function(req, res) {
  var conditions = { _id: req.params.userId };

  User.updateOne(conditions, {
    $push: {
      user_history: {
        heart_Beat: req.body.heart_Beat,
      }
    }
  }).select()
    .exec(doc => {  
    if (!doc) {
      return res.status(409).end();
    }
    return res.status(200).json({
      message: 'saved',
      result: doc
    })
  })   
}

这是我的架构:

const userSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  phoneNumber: { type: String, default: 0 },
  heartBeat: { type: Number, default: 0 },
  user_history: [
    {
      heart_Beat: { type: Number, default: 0 },
      date: { type: String, default: () => moment().format("dddd, MMMM Do YYYY, h:mm:ss a")}
    }
  ]
})

0 个答案:

没有答案