Basic crud operation

208 views

Published on

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

Published in: Technology, Business
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
208
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Basic crud operation

  1. 1. MongoDB Basic Shell Program’sCRUDOperations forMongoDB
  2. 2. Start the Shell• C:mongodb-win32-x86_64-2.2.3bin>mongo.exeMongoDB shell version: 2.2.3connecting to: test
  3. 3. Create Document the insert()> register={"uName":"anish",... "date":new Date()}{ "uName" : "anish", "date" : ISODate("2013-03-10T10:08:30.891Z") }> db.user.insert(register)> db.user.find(){ "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname" :"anish", "date" : ISODate("2013-03-10T09:52:53.144Z") }{ "_id" : ObjectId("513c5be21b37fce6f69eb9e2"), "uName" :"anish", "date" : ISODate("2013-03-10T10:08:30.891Z") }The “_id” field If you attempt to insert a document withoutthe _id field, the client library or the mongod instance will addan _id field and populate the field with a unique ObjectId.
  4. 4. Create Document the insert() withown “_id” key> register ={"_id":100,"uName":"arjun","date":new Date()}{"_id" : 100,"uName" : "arjun","date" : ISODate("2013-03-12T14:41:15.124Z")}>db.user.insert(register)> db.user.find({"uName":"arjun"}){ "_id" : 100, "uName" : "arjun", "date" :ISODate("2013-03-12T14:41:15.124Z") }
  5. 5. Create Document the insert() same “_id” keywill throw E11000 duplicate key error> register ={"_id":100,"uName":"arjun","date":new Date()}{"_id" : 100,"uName" : "arjun","date" : ISODate("2013-03-12T14:44:14.163Z")}> db.user.insert(register)E11000 duplicate key error index: test.user.$_id_dup key: { : 100.0 }
  6. 6. MongoDB Reading/findingDocumentReading/finding Document Use Casedetailed
  7. 7. Reading/finding in DocumentSyntaxdb.collection.find( <query>,<projection> )<query> argument corresponds tothe WHERE statement, and<projection> argument correspondsto the list of fields to select from theresult set.
  8. 8. Reading Document> db.user.findOne(){"_id" : ObjectId("513c581f1b37fce6f69eb9e0"),"fname" : "anish","date" : ISODate("2013-03-10T09:52:53.144Z")}> db.user.find(){ "_id" : ObjectId("513c581f1b37fce6f69eb9e0"), "fname": "anish", "date" : ISODate("2013-03-10T09:52:53.144Z") }{ "_id" : ObjectId("513c5be21b37fce6f69eb9e2"),"uName" : "anish", "date" : ISODate("2013-03-10T10:08:30.891Z") }Refer more use case on find() on the Other video
  9. 9. findOne()Find the first record in the document> db.user.findOne(){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [{"name" : "anish","rate" : "good"},{"name" : "nath","rate" : "bad"}],"uName" : "anish"}
  10. 10. findOne() : specify which key to return• Find the first record in the document and return only “_id”> db.user.findOne({},{"_id":1}){ "_id" : ObjectId("513c6ef1cfce9090d3fd8b1d") }• Find the first record in the document and return only“_id” and date> db.user.findOne({},{"_id":1,"date":1}){"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),"date" : ISODate("2013-03-10T11:30:50.555Z")}• Find the first record in the document and return all thedata except the “_id” field> db.user.findOne({},{"_id":0}){"date" : ISODate("2013-03-10T11:30:50.555Z"),"remarks" : [{"name" : "anish","rate" : "good"},{"name" : "nath","rate" : "bad"}],"uName" : "anish"}
  11. 11. finding in Subdocument>db.user.find({"remarks.name":"anish" }){ "_id" :ObjectId("513c6ef1cfce9090d3fd8b1d"),"date" : ISODate("2013-03-10T11:30:50.555Z"), "remarks" : [ {"name" : "anish", "rate" : "good" }, {"name" : "nath", "rate" : "bad" } ],"uName" : "anish" }
  12. 12. findOne()/find()• Summary on <K,boolean>specify <“k”,0> or <“K”,1>to show/hide data to bereturned from thecollection
  13. 13. MongoDB updating DocumentUpdating Document Use Casedetailed
  14. 14. 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") }
  15. 15. Updating DocumentSyntax : db.collection.update( <query>,<update>,<options> )> db.user.find(){ "_id" : ObjectId("513c60deb2080f893286d0a3"), "uName" : "anish", "date" : ISODate("2013-03-10T10:30:47.033Z") }{ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z") }> register.uName={"uName" : "nath"}{ "uName" : "nath" }> db.user.update({"uName":"anish"},register)> db.user.find(){ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z") }{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z"), "uName" : {"uName" : "nath" } }
  16. 16. 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}
  17. 17. 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"}}
  18. 18. 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"}}
  19. 19. Deleting Document> db.user.find(){ "_id" : ObjectId("513c60fab2080f893286d0a4"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z") }{ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z"), "uName" : {"uName" : "nath" } }>db.user.remove({"_id":ObjectId("513c60fab2080f893286d0a4")})> db.user.find(){ "_id" : ObjectId("513c60deb2080f893286d0a3"), "lName" : "anish", "date" : ISODate("2013-03-10T10:31:18.632Z"), "uName" : {"uName" : "nath" } }>db.user.remove() : deletes documentspermanently from the database
  20. 20. Thanks for Watchingfeedbackappreciated…………………………

×