1
    "ProductCategory": [
        {
          "_id": "6246e0d09b16cf549256ed75",
          "name": "Arduino Boards",
          "desc": "this is for demo purpose",
          "date": "2022-03-31T18:30:00.000Z",
          "products": [
            {
              "name": "Arduino Uno R3 CH340G",
              "imgUrl": "",
              "price": "799",
              "desc": "",
              "_id": "6246e1fb9b16cf549256ed77"
            },
            {
              "name": "Arduino Nano V3.0 ATMEGA 328P (with USB Cable)",
              "imgUrl": "",
              "price": "599",
              "desc": "",
              "_id": "6246e3049b16cf549256ed79"
            }
          ]
        },
        {
          "_id": "6246e32d9b16cf549256ed7d",
          "name": "Sensors",
          "desc": "this is for demo purpose",
          "date": "2022-03-31T18:30:00.000Z",
          "products": [
            {
              "name": "PU6050 Gyroscope Sensor",
              "imgUrl": "",
              "price": "129",
              "desc": "",
              "_id": "6246e3d29b16cf549256ed7f"
            },
            {
              "name": "CCS811 Carbon Monoxide Gas Sensor",
              "imgUrl": "",
              "price": "1799",
              "desc": "",
              "_id": "6246e3d29b16cf549234ea3r"
            }
          ]
        }

This is a data in mongoDB with the collection named as ProductCategory. I want to find the product with _id "6246e3049b16cf549256ed79" which is "Arduino Nano V3.0 ATMEGA 328P (with USB Cable)". How can I find it and also delete it.

nimrod serok
  • 14,151
  • 2
  • 11
  • 33

2 Answers2

1

In order to delete from a nested array, you can use pipeline update and $filter for this:

db.ProductCategory.update({
  "products._id": idToDelete
},
[
  {
    $set: {
      products: {
        $filter: {
          input: "$products",
          as: "item",
          cond: {$ne: ["$$item._id", idToDelete]}
        }
      }
    }
  }
])

This will filter the products array to contain only items that their id is not idToDelete.

You can see it works on the playground

In order to just find the document, you can use:

db.ProductCategory.find({"products._id": idToDelete})
nimrod serok
  • 14,151
  • 2
  • 11
  • 33
0
db.categories.deleteOne( {"_id": ObjectId("6246e3049b16cf549256ed79")});

https://www.mongodb.com/docs/manual/tutorial/remove-documents/

Blue
  • 22,608
  • 7
  • 62
  • 92
Strider
  • 134
  • 7
  • It is not able to find the product or delete the product. – Omkar Ghodake Apr 01 '22 at 13:14
  • Read the documentation https://www.mongodb.com/docs/manual/tutorial/remove-documents/ you can delete documents by id from your collection – Strider Apr 01 '22 at 13:27
  • Yes, but we can use this syntax for those which we can find at very outside of the document. The one which I want to find and delete is the nested one. Means there is a main object named "Arduino Board". We can find and delete it by this method. But inside it there is objects named "products" which is an array of objects. So to delete inside the products array is not possible by this syntax. – Omkar Ghodake Apr 01 '22 at 15:07