• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
 

Basic crud operation

on

  • 225 views

Visit

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

Statistics

Views

Total Views
225
Views on SlideShare
225
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Basic crud operation Basic crud operation Presentation Transcript

    • MongoDB Basic Shell Program’sCRUDOperations forMongoDB
    • Start the Shell• C:mongodb-win32-x86_64-2.2.3bin>mongo.exeMongoDB shell version: 2.2.3connecting to: test
    • 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.
    • 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") }
    • 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 }
    • MongoDB Reading/findingDocumentReading/finding Document Use Casedetailed
    • 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.
    • 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
    • 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"}
    • 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"}
    • 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" }
    • findOne()/find()• Summary on <K,boolean>specify <“k”,0> or <“K”,1>to show/hide data to bereturned from thecollection
    • MongoDB updating DocumentUpdating Document Use Casedetailed
    • 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") }
    • 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" } }
    • 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}
    • 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"}}
    • 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"}}
    • 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
    • Thanks for Watchingfeedbackappreciated…………………………