首页 > 解决方案 > MongoDp 不会更新对象数组

问题描述

第一篇在这里,我试图删除数组中的一个对象,如果它符合一些标准。这是我的模式:

const userSchema = new Schema({
name : {
    type:String,
    required: true,
    
}
,
lastname : {
    type:String,
    required: true,
    
}

,

cars :
    [  
        {
            _id: false,
            FirstPart:String,
            SecondPart:String
            
        }
    ]

这是我尝试使用 updateOne 方法删除对象的方式:

app.delete('/profile/:id/Cars', async (req,res)=>{

    const {id} =req.params
    console.log(req.body)
    
    console.log(req.body.FirstPart)
    console.log(req.body.SecondPart)
    console.log(id)
   const result = await User.updateOne(
        { _id:id },
        { $pull: { cars : { FirstPart: req.body.FirstPart, SecondPart: req.body.SecondPart } } },{ safe: true, multi:true }
      );
      console.log(result)
      res.send("dog")
})

它的用户通过 Passport 拥有自己的 id。打印的输出是:

{ FirstPart: ' XNH ', SecondPart: ' 1234 ' }
 XNH
 1234
6053231f45749903d066baf0
{ n: 1, nModified: 0, ok: 1 }

你有什么建议吗?提前致谢

标签: mongodbmongoose

解决方案


推荐阅读