Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 10 August 2013

MongoDB Crud Operations (Create, Retrieve, Update and Delete in MongoDB)

In our previous tutorial we came across how to 'Install, Setup and Run MongoDB in Windows'. In this particular blog we w'll see basic crud operation using Mongo Shell commands.


Create, Retrieve, Update and Delete operations in MongoDB

We will discuss how to create a database in MongoDB, how to create a table in MongoDb, how to insert data in a MongoDB Collection and how to update and delete data from MongoDB table(Collection)


Start MongoDB Shell from Command Line

Start MongoDB shell with 'mongod' command, you will see following screen as shown below.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\nagesh.chauhan>mongod
mongod --help for help and startup options
Sun Aug 11 00:19:32.887
Sun Aug 11 00:19:32.891 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Sun Aug 11 00:19:32.891
Sun Aug 11 00:19:32.931 [initandlisten] MongoDB starting : pid=8424 port=27017 dbpath=\data\db\ 32-bit host=GGN-F7-LO-D129
Sun Aug 11 00:19:32.932 [initandlisten]
Sun Aug 11 00:19:32.933 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sun Aug 11 00:19:32.933 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Sun Aug 11 00:19:32.934 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Sun Aug 11 00:19:32.935 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Sun Aug 11 00:19:32.935 [initandlisten]
Sun Aug 11 00:19:32.935 [initandlisten] db version v2.4.5
Sun Aug 11 00:19:32.936 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b
Sun Aug 11 00:19:32.936 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
Sun Aug 11 00:19:32.937 [initandlisten] allocator: system
Sun Aug 11 00:19:32.937 [initandlisten] options: {}
Sun Aug 11 00:19:33.049 [initandlisten] waiting for connections on port 27017
Sun Aug 11 00:19:33.049 [websvr] admin web console waiting for connections on port 28017



Create connection in MongoDB

Open an another instance of command prompt and connect MongoDB by supplying 'mongo' command, you will see following screen ready to work in Mongo.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\nagesh.chauhan>mongo
MongoDB shell version: 2.4.5
connecting to: test
Server has startup warnings:
Sat Aug 10 23:25:04.483 [initandlisten]
Sat Aug 10 23:25:04.483 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sat Aug 10 23:25:04.483 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Sat Aug 10 23:25:04.483 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Sat Aug 10 23:25:04.484 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Sat Aug 10 23:25:04.484 [initandlisten]
>



Show all Databases in MongoDB

To see all exixting database in MongoDB, supply 'show dbs' command, you will see following output containing all exiting database:

> show dbs
beingDB 0.0625GB
local   0.03125GB
nageshDB        0.0625GB
>



Create a Database in MongoDB

In MongoDB it is not required to create a database before using it, just supply 'use databaseName' and you will be switched to specified database even if the database don't exists, mongo will create the database on the fly at the time you use it.

> use nextDB
switched to db nextDB
>



Create a Table in MongoDB

Same as creating a database, we dont need to create a table before using it, just switch to the databse in which you want to create a new table and supply 'db.nextTable.insert({column1:"value",column2:"value",...})' the very first time you insert the data in a table Mongo will create the table on the fly if it does not exists already.

> use nextDB
switched to db nextDB
> db.nextTable.insert({id:1,name:"nagesh",email:"beingjavaguys",city:"new delhi"})
>



Insert Data in a Table(Collection) in MongoDB

To insert data in a table use command 'db.nextTable.insert({column1:"value",column2:"value",...})' or 'db.nextTable.save({column1:"value",column2:"value",...})', as shown below :

> use nextDB
switched to db nextDB
> db.nextTable.insert({id:1,name:"nagesh",email:"beingjavaguys",city:"new delhi"})
> db.nextTable.insert({id:2,name:"ankush",email:"being@javaguys.com",city:"noida"})
> db.newTable.save({id:3,name:"chauhan",email:"chauhan@javaguys.com",city:"new delhi"})



Select or Retrieve data from a Table(Collection) in MongoDB

To select all rows from a table supply command 'db.newTable.find()', you will get an output as shown below :

> db.newTable.find()
{ "_id" : ObjectId("520685313b1aad94e3bfa99d"), "id" : 1, "name" : "ankush", "email" : "being@javaguys.com", "city" : "noida" }
{ "_id" : ObjectId("5206855d3b1aad94e3bfa99e"), "id" : 2, "name" : "nagesh", "email" : "nagesh@javaguys.com", "city" : "new delhi" }
{ "_id" : ObjectId("520686b83b1aad94e3bfa99f"), "id" : 3, "name" : "chauhan", "email" : "chauhan@javaguys.com", "city" : "new delhi" }



Update data or row in a Table(Collection) in MongoDB

To update data or a row in a table supply command 'db.newTable.update({identifierColumn:value},{$set:{columnToChange:"newValue"}})', you will get an output as shown below :

> db.newTable.find()
{ "_id" : ObjectId("520685313b1aad94e3bfa99d"), "id" : 1, "name" : "ankush", "email" : "being@javaguys.com", "city" : "noida" }
{ "_id" : ObjectId("5206855d3b1aad94e3bfa99e"), "id" : 2, "name" : "nagesh", "email" : "nagesh@javaguys.com", "city" : "new delhi" }
{ "_id" : ObjectId("520686b83b1aad94e3bfa99f"), "id" : 3, "name" : "chauhan", "email" : "chauhan@javaguys.com", "city" : "new delhi" }
> db.newTable.update({id:1},{$set:{name:"ankita"}})
> db.newTable.find()
{ "_id" : ObjectId("520685313b1aad94e3bfa99d"), "id" : 1, "name" : "ankita", "email" : "being@javaguys.com", "city" : "noida" }
{ "_id" : ObjectId("5206855d3b1aad94e3bfa99e"), "id" : 2, "name" : "nagesh", "email" : "nagesh@javaguys.com", "city" : "new delhi" }
{ "_id" : ObjectId("520686b83b1aad94e3bfa99f"), "id" : 3, "name" : "chauhan", "email" : "chauhan@javaguys.com", "city" : "new delhi" }



Delete data or a row in Table(Collection) in MongoDB

To delete data or a row in a table supply command 'db.newTable.remove({identifierColumn:value})', you will get an output as shown below :

> db.newTable.find()
{ "_id" : ObjectId("520685313b1aad94e3bfa99d"), "id" : 1, "name" : "ankita", "email" : "being@javaguys.com", "city" : "noida" }
{ "_id" : ObjectId("5206855d3b1aad94e3bfa99e"), "id" : 2, "name" : "nagesh", "email" : "nagesh@javaguys.com", "city" : "new delhi" }
{ "_id" : ObjectId("520686b83b1aad94e3bfa99f"), "id" : 3, "name" : "chauhan", "email" : "chauhan@javaguys.com", "city" : "new delhi" }
> db.newTable.remove({id:1})
> db.newTable.find()
{ "_id" : ObjectId("5206855d3b1aad94e3bfa99e"), "id" : 2, "name" : "nagesh", "email" : "nagesh@javaguys.com", "city" : "new delhi" }
{ "_id" : ObjectId("520686b83b1aad94e3bfa99f"), "id" : 3, "name" : "chauhan", "email" : "chauhan@javaguys.com", "city" : "new delhi" }
>


In this particular blog we came across 'MongoDB Crud Operations (Create, Retrieve, Update and Delete in MongoDB)'. In upcoming blogs we will see more about 'MongoDb' and other opensource technologies.







Thanks for reading !
Being Java Guys Team




2 comments:

  1. Hi Nagesh, this is really good information. If I need to contact you, can I have your email for few of my questions.
    - Navin

    ReplyDelete
  2. db.newTable.find() is not correct command, The Correct Command is db.nextTable.find()

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft