-1

I working on NodeJS and MongoDB and I have database JSON like this

{
   _id: "123"
   classroom: [
    {
      classId: "2046"
      className: "10b"
    },
    {
      classId: "2079"
      className: "12b"
    }   
  ]
},

{
   _id: "456"
   classroom: [
    {
      classId: "2069"
      className: "36b"
    },
    {
      classId: "2011"
      className: "36c"
    }   
  ]
}

I want to delete classroom on

_id: "123" and have classId: "2046"

,so after delete process, I will have JSON like this

   {
       _id: "123"
       classroom: [
        {
          classId: "2079"
          className: "12b"
        }   
      ]
    },

    {
       _id: "456"
       classroom: [
        {
          classId: "2069"
          className: "36b"
        },
        {
          classId: "2011"
          className: "36c"
        }   
      ]
    }

So please help me to do that, any advices are very welcome thank you

fahmika
  • 3
  • 1
  • I think this is the similar question [stackoverflow link](https://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb) – Akash Srivastava Apr 16 '18 at 08:41

1 Answers1

0

You can use this query to do that :

db.collection.update({
    _id: '123'
}, {
    $pull: {
        classroom: {
            classId: '2046'
        }
    }
});
Nicolas
  • 457
  • 5
  • 15