MongoDB – How to remove a field from document

This MongoDB article shows you how to remove a field from document and array.

1. Remove a field from Documents

Sample of document, and you want to remove the field “affLink”.

domain.json
{
      "_id" : 1,
      "domain" : "mkyong.com",
      "affLink" : "abc"
}

To remove a field from all documents, set {multi: true}, else only the field of the first matched document will be removed.


db.domain.update({},{$unset: {affLink:1}},{multi: true});

Output

domain.json
{
      "_id" : 1,
      "domain" : "mkyong.com"
}

2. Remove a field from Array

Prior to MongoDB 2.6, there is still no official function to remove a field from the array. To fix it, you need to write a script :

person.json
{
   _id: 1,
   name: "mkyong",
   addresses: [
	{
	  street: "99 The Rock Street",
	  city: "Boston",
	  state: "MA",
	  zip: "66666"
	},
	{
	  street: "88 WWF Street",
	  city: "Boston",
	  state: "MA",
	  zip: "77777"
	}
   ]
}

Loop over the documents and remove field “state” from the array one by one.

 
 db.person.find({}).forEach(function(doc) {

	var address = doc.addresses;
	for(var i = 0; i < address.length; ++i) { 
		var x = address[i];
		delete (x["state"]);
		
	}
	db.person.save(doc);

});

Output

person.json
{
   _id: 1,
   name: "mkyong",
   addresses: [
	{
	  street: "99 The Rock Street",
	  city: "Boston",
	  zip: "66666"
	},
	{
	  street: "88 WWF Street",
	  city: "Boston",
	  zip: "77777"
	}
   ]
}

References

  1. MongoDB : $unset
  2. MongoDB : Modify documents
  3. MongoDB : Query document, Array

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Samarpit Arya Recent comment authors
newest oldest most voted
Samarpit Arya
Guest
Samarpit Arya

Hello Mkyong,
As “_id” field is immutable. There is any concept for removing “_id” field from a specific collection. I want a collection without having id field. Please guide me if there is solution._