首页 > 解决方案 > 猫鼬 findOneAndUpdate 不从数组中删除对象

问题描述

我遇到了猫鼬“findOneAndUpdate”查询的问题。以下是我的文档的架构结构。

{
  "_id": "5fe2b6145dcd48c505000590",
  "email": "user1@mail.com",
  "todos": [
    {
      "_id": "5fe2b990fb9d7055c46d83a4",
      "text": "important task",
      "status": "in-progress"
    }
  ]
}

我试图通过该特定对象的email和从对象的“待办事项”数组中删除待办事项对象。_id以下是我正在使用的代码。

let { task_id, email } = req.body


UserModel.findOneAndUpdate({ email : email},
{
    $pull : {
        todos : {
                _id : task_id
        }
    }
},
{ safe: true, new: true },
function(err, model) {
    if(err){
        return res.send(err)
    }
    return res.send(model)
}
)

上面的代码只是返回预设文档,而不是从todos数组中删除对象。

上面的代码有什么问题?

标签: node.jsmongoose

解决方案


推荐阅读