Mongoose-查找对象并在数组中删除Objeto
我有此文档:
[
{
"_id": "626c5fd2aa0fc0e4eef45c94",
"productos": [
{
"_id": "626d50b621180e291a330333",
"nombre": "ciruelas",
"descripcion": "marca lala",
"codigo": 33,
"foto": "https://cdn3.iconfinder.com/data/icons/fruits-52/150/icon_fruit_morango-256.png",
"precio": 15,
"stock": 30,
"timestamp": "2022-04-30T15:06:45.128Z",
"__v": 0
}
],
"timestamp": "2022-04-29T21:59:35.301Z",
"__v": 0
},
{
"_id": "626d508b21180e291a33032c",
"productos": [],
"timestamp": "2022-04-30T15:06:45.139Z",
"__v": 0
}
]
我想用“ _id”删除“ ciruela”:“ 626D50B621180E291A330333” insiral“ prodosos”。
我该怎么办?
I have this document :
[
{
"_id": "626c5fd2aa0fc0e4eef45c94",
"productos": [
{
"_id": "626d50b621180e291a330333",
"nombre": "ciruelas",
"descripcion": "marca lala",
"codigo": 33,
"foto": "https://cdn3.iconfinder.com/data/icons/fruits-52/150/icon_fruit_morango-256.png",
"precio": 15,
"stock": 30,
"timestamp": "2022-04-30T15:06:45.128Z",
"__v": 0
}
],
"timestamp": "2022-04-29T21:59:35.301Z",
"__v": 0
},
{
"_id": "626d508b21180e291a33032c",
"productos": [],
"timestamp": "2022-04-30T15:06:45.139Z",
"__v": 0
}
]
I would like to delete "ciruela" with "_id": "626d50b621180e291a330333" inside "productos".
How could i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$ pull
从数组Productos
中删除用指定的_id
删除对象=“ https://mongoplayground.net/p/tsojypb5y6q” rel =“ nofollow noreferrer”> https://mongoplayground.net.net/p/tsojypb5y6q
You could use
$pull
to remove the object with specified_id
from the arrayproductos
Here is the link to the code for your reference https://mongoplayground.net/p/tsOJypb5Y6q
解决!
我不得不先寻找我的“产品”。
然后,当我做
findbyidandupdate
时,请拉该产品。我不知道那是正确的解决方案,但对我有用。
Solved!
I had to look for my "product" first.
And then, pull that product when I do
findByIdAndUpdate
.I don´t know if that is the correct solution, but it worked for me.