mongodb - 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 }
你有什么建议吗?提前致谢
解决方案
推荐阅读
- ruby - 字符串包括奇怪的行为
- r - 添加 p 值以比较组意味着在 gganimate gif 中使用箱线图/小提琴有时会有所不同
- python - Pylint 和 mypy 意见在继承静态方法上发生冲突
- java - Android Studio:自定义编写器类扩展了 BufferedWriter (PemWriter)
- spring - 为什么我需要 Spring Boot `messages.properties` 而不仅仅是 `messages_en.properties`?
- tcl - 计算给定数组之和的函数
- python - Tensorflow - 准备一个 CSV 文件来提供循环神经网络
- c++ - 每次打印出一个向量都会产生不同的结果。它还打印不在向量中的随机代码
- scala - 访问类/特征与对象中的类型定义
- excel - VLOOKUP 公式寻求 - 我的一个不工作