Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mongo db updatedocumentusecases

107 views

Published on

Visit
http://www.zariga.com
http://www.youtube.com/user/zarigatongy

Mongo db updatedocumentusecases

  • Be the first to like this

Mongo db updatedocumentusecases

  1. 1. MongoDB updating DocumentUpdating Document Use Casedetailedzariga.com
  2. 2. Updating Document <K,V>> db.user.find(){ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"), "uName" : "nath", "date" :ISODate("2013-03-10T11:30:50.555Z") }Update the uName with “anish”Step 1 find userName with “nath”> var test=db.user.findOne({"uName":"nath"})Step 2> test.uName="anish"anishStep 3>db.user.update({"uName":"nath"},test)> db.user.findOne(){ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish", "date" : ISODate("2013-03-10T11:30:50.555Z") }zariga.com
  3. 3. Updating Document adding a new <k,V> to the document> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "nath","date" : ISODate("2013-03-10T11:30:50.555Z")}Add a age in the document i.e{"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish","date" : ISODate("2013-03-10T11:30:50.555Z"),"age" : 32}Step 1 : findByIdvar findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")})Step2 : add age to the document> findById.age=3232Step 3 Update the Document> db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById)> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish","date" : ISODate("2013-03-10T11:30:50.555Z"),"age" : 32}zariga.com
  4. 4. Updating Document :Add a new Structure in the existing Document> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish","date" : ISODate("2013-03-10T11:30:50.555Z"),"age" : 32}Change the Document to the following{"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish","date" : ISODate("2013-03-10T11:30:50.555Z"),"age" : 32,"family" : {"motherName" : "Indu","wifeName" : "manisha","fatherName" : "Anil"}}zariga.com
  5. 5. Updating Document :Add a new Structure in the existing DocumentStep 1 : findUserById>var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")})Step 2: Add a family JSON entry>findById.family={"motherName":"Indu","wifeName":"manisha","fatherName":"Anil"}{ "motherName" : "Indu", "wifeName" : "manisha", "fatherName" : "Anil" }Step 3 : Update the docuemt :> db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")},findById)Step 4 : Search the User DB> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"uName" : "anish","date" : ISODate("2013-03-10T11:30:50.555Z"),"age" : 32,"family" : {"motherName" : "Indu","wifeName" : "manisha","fatherName" : "Anil"}}zariga.com
  6. 6. Thanks for Watchingfeedbackappreciated…………………………zariga.com

×