Search code examples
javascriptnode.jsdatabasemongodbcrud

How to Delete a Object from Array in MongoDB?


Here is my MongoDB Collection :

{
  "_id": {
    "$oid": "63e9e567a71412b168b38172"
  },
  "Love": [
    {
      "key": 1,
      "title": "Love",
      "content": "Q. How many programmers does ittake to change a light bulb? A. None – It’s a hardware problem"
    },
    {
      "key": 2,
      "title": "Love",
      "content": "How to keep a programmer in the shower forever.Show him the shampoo bottle instructions: Lather. Rinse. Repeat."
    }
  ],
  "Happy": [
    {
      "key": 1,
      "title": "HAPPY",
      "content": "Q. Why did the programmer quit his job? A. Because he didn't get arrays."
    },
    {
      "key": 2,
      "title": "HAPPY",
      "content": "What's the difference between hardware and software? You can hit your hardware with a hammer, but you can only curse at your software."
    }
  ],
  "Dull": [
    {
      "key": 1,
      "title": "DULL",
      "content": "Job trails from HCL"
    },
    {
      "key": 2,
      "title": "DULL",
      "content": "Job trails from NOWhere"
    }
  ],
  "Angry": [
    {
      "key": 1,
      "title": "ANGRY",
      "content": "Q. How many programmers doesit take to change a light bulb? A. None – It’s a hardware problem"
    },
    {
      "key": 2,
      "title": "ANGRY",
      "content": "Job trails from NOWhere"
    }
  ]
}

I am getting the values "Love or Dull or Angry or Happy" and also the key that needs to be deleted from the User. Using these values I am trying to delete that perticular field.

For Example : I get ("Love", 1) as req.body, my O/P should be :

{
  "_id": {
    "$oid": "63e9e567a71412b168b38172"
  },
  "Love": [
    {
      "key": 2,
      "title": "Love",
      "content": "How to keep a programmer in the shower forever.Show him the shampoo bottle instructions: Lather. Rinse. Repeat."
    }
  ],
  "Happy": [
    {
      "key": 1,
      "title": "HAPPY",
      "content": "Q. Why did the programmer quit his job? A. Because he didn't get arrays."
    },
    {
      "key": 2,
      "title": "HAPPY",
      "content": "What's the difference between hardware and software? You can hit your hardware with a hammer, but you can only curse at your software."
    }
  ],
  "Dull": [
    {
      "key": 1,
      "title": "DULL",
      "content": "Job trails from HCL"
    },
    {
      "key": 2,
      "title": "DULL",
      "content": "Job trails from NOWhere"
    }
  ],
  "Angry": [
    {
      "key": 1,
      "title": "ANGRY",
      "content": "Q. How many programmers doesit take to change a light bulb? A. None – It’s a hardware problem"
    },
    {
      "key": 2,
      "title": "ANGRY",
      "content": "Job trails from NOWhere"
    }
  ]
}

Thanks in advance.

Any mongoDb function I tried, I am getting errors. I am not able to delete it no matter what.


Solution

  • I feel a bit bad helping you delete jokes from your database. I hope you have no problem inserting more jokes.

    Anyway, to delete your specific array element, you could update your collection like this.

    db.collection.update({
      "_id": ObjectId("63e9e567a71412b168b38172")
    },
    {
      "$pull": {
        "Love": {
          "key": 1
        }
      }
    })
    

    Try it on mongoplayground.net.