Monday 1 July 2013

Simple CRUD with MongoDB


Create Database:

        You don't really create a database with mongo, you just kind of start using it. Once you put something in there, it exists. I'm going to name my new database  'employee'

                   > use  employee
                    switched to db employee

Adding Data:

   To do anything in mongo, you start your command with db which refers to the database you're using. The different parts of the command are separated by dots. To insert data you use a command like db.[collection].save() and feed in the data to save. The format of the data is JSON-esque - I read JSON but I don't really write it, however I found it became familiar pretty quickly. To insert some data, you can do

 >db.new_employee.save({name:'Gowthaman',mobilno:'8870226416',email:'gowthaman.ideas2it@gmail.com'});
>db.new_employee.save({name:'Prem',mobilno:'123456',email:'prems.ideas2it@gmail.com'}) ;

Fetching Data

   Did anything happen? We can check, using db.[collection].find() - this will give us everything in the collection, a bit like select * from [table] does in SQL.

  >db.new_employee.find(); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "name" : "Gowthaman", "mobilno" : "8870226416", "email" : "gowthaman.ideas2it@gmail.com" } 
{ "_id" : ObjectId("51d13039d4c11d09b2f1f118"), "name" : "Prem", "mobilno" : "123456", "email" : "prems.ideas2it@gmail.com" } 

We definitely have data! We can also filter this down, the equivalent of adding a "where" clause, for

> db.new_employee.find({name:'Gowthaman'}); 

{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "name" : "Gowthaman", "mobilno" : "8870226416", "email" : "gowthaman.ideas2it@gmail.com" } 

You can add multiple constraints here,

>db.new_employee.find({name:'Gowthaman',email:'gowthaman.ideas2it@gmail.com'}); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "name" : "Gowthaman", "mobilno" : "8870226416", "email" : "gowthaman.ideas2it@gmail.com" }

Updating Data

Find by name

> db.new_employee.find({name:'Gowthaman'}); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "name" : "Gowthaman", "mobilno" : "8870226416", "email" : "gowthaman.ideas2it@gmail.com" } 

Update email   by name 

> db.new_employee.update({name:'Gowthaman'},{$set {email:'vimalgowthaman14@gmail.com'}}); 

Verify record update 

> db.new_employee.find({name:'Gowthaman'}); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "email" : "vimalgowthaman14@gmail.com", "mobilno" : "8870226416", "name" : "Gowthaman" } 


Deleting Data

Find  all records

  > db.new_employee.find(); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "email" : "vimalgowthaman14@gmail.com", "mobilno" : "8870226416", "name" : "Gowthaman" } 
{ "_id" : ObjectId("51d13039d4c11d09b2f1f118"), "name" : "Prem", "mobilno" : "123456", "email" : "prems.ideas2it@gmail.com" } 


Delete Record by name

> db.new_employee.remove({name:'Prem'}); 

verify record deleted

> db.new_employee.find(); 
{ "_id" : ObjectId("51d1301bd4c11d09b2f1f117"), "email" : "vimalgowthaman14@gmail.com", "mobilno" : "8870226416", "name" : "Gowthaman" } 
   

Refer: getting-started-with-mongodb

No comments:

Post a Comment