2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Remove element or field permanently from collection MongoDB using Java

I have a collection created under MongoDB which contain some element. And my requirement it is, I want to do some delete operation on that collection. But instead of setting the value to null or empty of an element need to delete that element permanently.

e.g collection COLLECTION_ONE and having element ELEMENT_ONE with value "ValueOne". instead of set the value of element to null or empty I want to delete ELEMENT_ONE from COLLECTION_ONE


If you know the use of $et attribute how to use in Java then its quite easy to remove a field from the document too. MongoDB provided another attribute $unset which is quite opposite to $set.So just use $unset with empty value your field will automatically remove.

For more information please refer to MongoDB official document https://docs.mongodb.com/manual/reference/operator/update/unset/

Answer is