Feature news

03. MongoDB - CRUD operations INSERT


# MongoDB CRUD Introducion 

MongoDB stores data in the form of documents, which are JSON-like field and value pairs. Documents are analogous to structures in programming languages that associate keys with values, where keys may hold other pairs of keys and values.MongoDB documents are BSON documents, which is a binary representation of JSON with additional type information.

# MongoDB CRUD operations 

1. Create        insert            
2. Read          find     
3. Update       update            
4. Delete        remove

# JSON examples   {"key":"value"} 

{"name":"Flintstone" ; "occupation":"Miner" ; "wife":"Wilma"}
 {"name":"Flintstone" , "occupation":"Miner" , "wife":"Wilma"}

{"title":"Star Wars" , "quotes":["Use The Force", "These are not the Droids you are looking for"] ,"director":"George Lucas"}

{"a":1, "b":{"s":1, "c":"foo", "d":"bar", "e":[1,2,"zajith",4]}}

#Mongo shell insert used this code db.<collection name>.insert() 

>doc={"name":"mohamedzajith", "mobile no":0756712419}
{"name":"mohamedzajith", "mobile no":0756712419}
>doc
{"name":"mohamedzajith", "mobile no":0756712419}
>
>db.student.insert(doc)
>
>db.student.find()

{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
>

or

>db.student.insert({"name":"mohamedzajith", "mobile no":0756712419})
>
>db.student.find()
{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
>
>db.student.insert({"name":"Androwsen", "mobile no":0778412645})
>db.student.insert({"name":"Kumaran", "mobile no":0756340957})
>db.student.find()
{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "mobile no":0778412645}
{ "_id" : ObjectId("................"),  "name":"Kumaran", "mobile no":0756340957 }

Mongo shell INSERT in to the array  
db.<collection name>.insert({"key":["value","value",.....,"value"]}) 

>db.student.insert({"name":"Androwsen", "like":["Apple","Banana","ice-cream"]})
>db.student.find()
{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "mobile no":0778412645}
{ "_id" : ObjectId("................"),  "name":"Kumaran", "mobile no":0756340957 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "like":["Apple","Banana","ice-cream"] }
>db.student.insert({"name":"Fahim", "like":["ice-cream","Grapes","Wood apple"]})
>db.student.find()
{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "mobile no":0778412645}
{ "_id" : ObjectId("................"),  "name":"Kumaran", "mobile no":0756340957 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "like":["Apple","Banana","ice-cream"] }
{ "_id" : ObjectId("................"),  "name":"Fahim", "like":["ice-cream","Grapes","Wood apple"] }
>db.student.insert({"name":"Niroshan", "like":["ice-cream","Grapes","Wood apple"],"work":[6,4,3,5,"per day"]})
>db.student.find()
{ "_id" : ObjectId("................"), "name":"mohamedzajith", "mobile no":0756712419 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "mobile no":0778412645}
{ "_id" : ObjectId("................"),  "name":"Kumaran", "mobile no":0756340957 }
{ "_id" : ObjectId("................"),  "name":"Androwsen", "like":["Apple","Banana","ice-cream"] }
{ "_id" : ObjectId("................"),  "name":"Fahim", "like":["ice-cream","Grapes","Wood apple"] }
{ "_id" : ObjectId("................"),  "name":"Niroshan", "like":["ice-cream","Grapes","Wood apple"] , "work":[6,4,3,5,"per day"] }

Next ===> MongoDB -CRUD Operation find()


0 comments: