Feature news

14. MongoDB - CRUD operations - update the fileds using $set , $unset




dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes

/*  find the all document from info collection  */
> db.info.find()
{ "_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }

/*  find the document '_id' equal 'fahimfana' from info collection  */
> db.info.find({ "_id" : "fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 22,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}

/*  update document from '_id' equal 'fahimfana'  ==> this is wrong way because erase other details  can be use $set command  */
> db.info.update({_id:"fahimfana"},{age:18})
>
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "age" : 18 }
> db.info.find()
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "fahimfana", "age" : 18 }

> db.info.update({_id:"fahimfana"},{locatin:"Srilanka"})
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "locatin" : "Srilanka" }

/*  insert a document '_id':'fahimfana'   */
> db.info.insert({"_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } })

> db.info.find({ "_id" : "fahimfana"}).pretty()
{
 "_id" : "fahimfana",
 "age" : 22,
 "email" : {
  "personal" : "mohamedfahim91@gmail.com",
  "offcial" : "fahimfana91@gmail.com"
 },
 "address" : {
  "street" : "hettuwewa",
  "city" : "anuradapura",
  "country" : "srilanka"
 }
}

#  Query using $set()
Use the $set operator to replace the value of a field to the specified value. If the field does not exist, the $set operator will add the field with the specified value.

> db.info.update({_id:"fahimfana"},{$set:{age:18}})
>
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 18,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}
> db.info.update({_id:"fahimfana"},{$set:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"business" : "fahimPvt@gmail.com",
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}

#  Query using $unset()
The $unset operator deletes a particular field. The specified value in the $unset expression (i.e. "" below) does not impact the operation.

> db.info.update({_id:"fahimfana"},{$unset:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}
>


0 comments: