How to drop a field from a MongoDB document ?

For people coming from RDBMS you must be remembering the alter table command for dropping/adding columns in a table.
In MongoDB it is possible to do this operation on some of the documents (rows) or all documents together.
There is no such thing as dropping a column from some rows in RDBMS either you drop it for table or don’t do it at all.
 
Let’s say we want to drop the location field from one document only. This is how we do it :
 
Swith to database called Google
 
rs1:PRIMARY> use Google
switched to db Google
rs1:PRIMARY>
 
Load some dummy data as below.
 
rs1:PRIMARY> for(var i = 1; i <=5 ; i++){db.users.insert({"_id" : i , "name" : "HR", "location" : "Pune" }); sleep(1);}
null
rs1:PRIMARY> db.users.find()
{ "_id" : 1, "name" : "HR", "location" : "Pune" }
{ "_id" : 2, "name" : "HR", "location" : "Pune" }
{ "_id" : 3, "name" : "HR", "location" : "Pune" }
{ "_id" : 4, "name" : "HR", "location" : "Pune" }
{ "_id" : 5, "name" : "HR", "location" : "Pune" }
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> for(var i = 6; i <=10 ; i++){db.users.insert({"_id" : i , "name" : "Tech", "location" : "Pune" }); sleep(1);}
null
rs1:PRIMARY>
rs1:PRIMARY> db.users.find()
{ "_id" : 1, "name" : "HR", "location" : "Pune" }
{ "_id" : 2, "name" : "HR", "location" : "Pune" }
{ "_id" : 3, "name" : "HR", "location" : "Pune" }
{ "_id" : 4, "name" : "HR", "location" : "Pune" }
{ "_id" : 5, "name" : "HR", "location" : "Pune" }
{ "_id" : 6, "name" : "Tech", "location" : "Pune" }
{ "_id" : 7, "name" : "Tech", "location" : "Pune" }
{ "_id" : 8, "name" : "Tech", "location" : "Pune" }
{ "_id" : 9, "name" : "Tech", "location" : "Pune" }
{ "_id" : 10, "name" : "Tech", "location" : "Pune" }
rs1:PRIMARY>
rs1:PRIMARY>
 
 
 
We are going to use $unset for dropping the field and below is the example which demonstrates that :
 
rs1:PRIMARY> db.users.update({name:'HR'},{$unset:{location:""}});
rs1:PRIMARY>
rs1:PRIMARY> db.users.find()
{ "_id" : 1, "name" : "HR" }
{ "_id" : 2, "name" : "HR", "location" : "Pune" }
{ "_id" : 3, "name" : "HR", "location" : "Pune" }
{ "_id" : 4, "name" : "HR", "location" : "Pune" }
{ "_id" : 5, "name" : "HR", "location" : "Pune" }
{ "_id" : 6, "name" : "Tech", "location" : "Pune" }
{ "_id" : 7, "name" : "Tech", "location" : "Pune" }
{ "_id" : 8, "name" : "Tech", "location" : "Pune" }
{ "_id" : 9, "name" : "Tech", "location" : "Pune" }
{ "_id" : 10, "name" : "Tech", "location" : "Pune" }
rs1:PRIMARY>
 
You can see that it only updated one document in above case. Let’s add the location field and try to do an update on all documents.
 
rs1:PRIMARY> db.users.update({name:'HR'},{$set:{'location':'Delhi'}},{multi:true})
rs1:PRIMARY>
rs1:PRIMARY> db.users.find()
{ "_id" : 1, "location" : "Delhi", "name" : "HR" }
{ "_id" : 2, "location" : "Delhi", "name" : "HR" }
{ "_id" : 3, "location" : "Delhi", "name" : "HR" }
{ "_id" : 4, "location" : "Delhi", "name" : "HR" }
{ "_id" : 5, "location" : "Delhi", "name" : "HR" }
{ "_id" : 6, "name" : "Tech", "location" : "Pune" }
{ "_id" : 7, "name" : "Tech", "location" : "Pune" }
{ "_id" : 8, "name" : "Tech", "location" : "Pune" }
{ "_id" : 9, "name" : "Tech", "location" : "Pune" }
{ "_id" : 10, "name" : "Tech", "location" : "Pune" }
rs1:PRIMARY>
 
 
I have intentionally updated location to Delhi for all records here.
Now let’s drop the location field from all documents.
 
 
rs1:PRIMARY> db.users.update({name:'HR'},{$unset:{location:""}},{multi:true})
rs1:PRIMARY> //multi will apply this change on all matching documents
rs1:PRIMARY> db.users.find()
{ "_id" : 1, "name" : "HR" }
{ "_id" : 2, "name" : "HR" }
{ "_id" : 3, "name" : "HR" }
{ "_id" : 4, "name" : "HR" }
{ "_id" : 5, "name" : "HR" }
{ "_id" : 6, "name" : "Tech", "location" : "Pune" }
{ "_id" : 7, "name" : "Tech", "location" : "Pune" }
{ "_id" : 8, "name" : "Tech", "location" : "Pune" }
{ "_id" : 9, "name" : "Tech", "location" : "Pune" }
{ "_id" : 10, "name" : "Tech", "location" : "Pune" }
rs1:PRIMARY>

  • Ask Question