MongoDB updating Document Updating Document Use Case detailed zariga.com
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" anish Step 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
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 : findById var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step2 : add age to the document > findById.age=32 32 Step 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
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
Updating Document :Add a new Structure in the existing Document Step 1 : findUserById >var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step 2: Add a family JSON entry >findById.family={"motherName":"Indu","wifeName":"manisha","fatherName":"A nil"} { "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
Thanks for Watching feedback appreciated……………… ………… zariga.com

Mongo db updatedocumentusecases

  • 1.
    MongoDB updating Document UpdatingDocument Use Case detailed zariga.com
  • 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" anish Step 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.
    Updating Document addinga 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 : findById var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step2 : add age to the document > findById.age=32 32 Step 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.
    Updating Document :Adda 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.
    Updating Document :Adda new Structure in the existing Document Step 1 : findUserById >var findById=db.user.findOne({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}) Step 2: Add a family JSON entry >findById.family={"motherName":"Indu","wifeName":"manisha","fatherName":"A nil"} { "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.