Mongoose-查找对象并在数组中删除Objeto

发布于 2025-01-25 17:18:03 字数 875 浏览 1 评论 0原文

我有此文档:

[
    {
        "_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2025-02-01 17:18:03

您可以使用$ pull从数组Productos中删除用指定的_id删除

db.collection.update({
  "_id": "626c5fd2aa0fc0e4eef45c94"
},
{
  "$pull": {
    "productos": {
      "_id": "626d50b621180e291a330333"
    }
  }
})

对象=“ 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 array productos

db.collection.update({
  "_id": "626c5fd2aa0fc0e4eef45c94"
},
{
  "$pull": {
    "productos": {
      "_id": "626d50b621180e291a330333"
    }
  }
})

Here is the link to the code for your reference https://mongoplayground.net/p/tsOJypb5Y6q

时光匆匆的小流年 2025-02-01 17:18:03

解决!

我不得不先寻找我的“产品”。

async eraseMyProductFromCart (idProduct, idCart) {
        try{      
            const productSelected = await Product.findOne({_id:idProduct})
            await Cart.findByIdAndUpdate(idCart, {$pull: {"products": productSelected}})
            return Cart.find()                      
        }
        catch(error){
            console.log(error.message)
        }
    }

然后,当我做findbyidandupdate时,请拉该产品。

我不知道那是正确的解决方案,但对我有用。

Solved!

I had to look for my "product" first.

async eraseMyProductFromCart (idProduct, idCart) {
        try{      
            const productSelected = await Product.findOne({_id:idProduct})
            await Cart.findByIdAndUpdate(idCart, {$pull: {"products": productSelected}})
            return Cart.find()                      
        }
        catch(error){
            console.log(error.message)
        }
    }

And then, pull that product when I do findByIdAndUpdate.

I don´t know if that is the correct solution, but it worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文