Friday 20 February 2015

mongoDB : $unset : Removes field


$unset operator is used to remove a filed from document.

Syntax
{ $unset: { <field1>: "", ... } }

> db.employee.find()
{ "_id" : 1, "firstName" : "Jessi" }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Hari Krishna", "lastName" : "Gurram", "age" : 25 }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Amara", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ], "age" : 30 }
{ "_id" : 6, "lastName" : "Amara", "age" : 31 }
{ "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" }
{ "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 }
{ "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] }


1.Remove the fields salary and hobbies from employee, where id is 3.
> db.employee.update({"_id" : 3}, {$unset : {"salary" : "", "hobbies" : ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.employee.find({"_id" : 3})
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu" }

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment