Swaroop's...

Hey! I am Swaroop SM. I enjoy writing code. Read more...

Basic operations in MongoDB

Monday, 22 October 2012

Off late, I’ve playing with MongoDB and I MongoDB’s key/value store was very impressive to me.

If you do not have the lathttp://docs.mongodb.org/manual/installation/est version of MongoDB, follow the instructions here: MongoDB Installation

Now let’s look at some basic operations in MongoDB:

Start Mongo shell and create a database called swaroop:
$ mongo
> use swaroop
> showdbs
	local	(empty)
	test	0.203125GB

MongoDB is lazy, so the resulting database created won’t show up unless you insert some data into it.

Insert data into MongoDB

Assume you need to insert the following data:
	name       : "Swaroop SM",
	gender	: "Male",
	website    : "swaroopsm.github.com"

To insert data into mongo, we need to pass data as JSON and then use the ‘save’ statement:

> db.persons.save({"name": "Swaroop SM",
				"gender": "Male",
  				"website": "swaroopsm.github.com"
			     })
In the above example, we are inserting data into the persons collection of the database swaroop. Remember, a collection can be considered as a table in RDBMS, although since MongoDB is a schema-free database, there is no concept of tables.

Insert an array of values:
	name       : "Swaroop SM",
	gender	: "Male",
	website    : "swaroopsm.github.com",
	friends	: "Joe", "Chris", "Kathy", "Thomas"
> db.persons.save({"name": "Swaroop SM",
				"gender": "Male",
  				"website": "swaroopsm.github.com",
  				"friends": ["Joe", "Chris", "Kathy", "Thomas"]
			     })
Complex data insertion:
	name       : "Swaroop SM",
	gender	: "Male",
	website    : "swaroopsm.github.com",
	friends	: "Joe", "Chris", "Kathy", "Thomas"
	
	Now we also need to store the friend's details of Swaroop SM.
	For e.g.: Joe's website, gender etc.
> db.persons.save({"name": "Carl James",
				"gender": "Male",
  				"website": "carljames.in",
  				"friends": {
  					"name": "Joe",
  					"gender": "Male",
  					"website": "joe.abc.com"
  					},
  					{
  					"name": "Chris",
  					"gender": "Male",
  					"website": "chris.xyz.com"
  					},
  					{
  					"name": "Kathy",
  					"gender": "Female",
  					"website": "kathy.pqr.org"
  					},
  					{
  					"name": "Thomas",
  					"gender": "Male",
  					"website": "Thomas.ttt.in"
  					}
			     })

Select Data from MongoDB

MongoDB uses the find method to retreive data.

Retrieve all documents of a particular collection:
> db.persons.find()
Result:
{ "_id" : ObjectId("5084dbc63a54d35a3ce18ae0"), "name" : "Swaroop SM", "gender" : "Male", "website" : "swaroopsm.github.com", "friends" : [ "Joe", "Chris", "Kathy", "Thomas" ] }
{ "_id" : ObjectId("5084e17f3a54d35a3ce18ae1"), "name" : "Johnson", "gender" : "Male", "website" : "johnson.czx.in", "friends" : [ "Swaroop SM", "Kristy" ] }
Retrieve a specific document:
> db.persons.find({"name": "Swaroop SM"})

This retreives all documents with the name field having the value Swaroop SM.

Result:
{ "_id" : ObjectId("5084dbc63a54d35a3ce18ae0"), "name" : "Swaroop SM", "gender" : "Male", "website" : "swaroopsm.github.com", "friends" : [ "Joe", "Chris", "Kathy", "Thomas" ] }
Retrieve a specific field in a document
> db.persons.find({"name":"Swaroop SM"},{"friends":1})

This retreives all the friends of Swaroop SM.

Result
{ "_id" : ObjectId("5084dbc63a54d35a3ce18ae0"), "friends" : [ "Joe", "Chris", "Kathy", "Thomas" ] }
Retrieve a specific field of a nested document
> db.persons.find({"name": "Carl James"},{"friends.website":1})

This retreives a all the websites of Carl James’ friends.

Result
{ "_id" : ObjectId("5084e49d3a54d35a3ce18ae2"), "friends" : [ { "website" : "joe.abc.com" }, 	{ "website" : "chris.xyz.com" }, { "website" : "kathy.pqr.org" }, { "website": "thomas.ttt.in" } ] }
Retrieve documents using operators
> db.persons.save({"name": "Smith","salary": 40000})
> db.persons.save({"name": "Chad","salary": 45000})

> db.persons.find({"salary": {$gt: 40000}})

This retieves all the documents whose salary field value is greater that 40000.

There are various operators like $lt, $ne etc. Check out the complete list here: MongoDB Conditional Operators

Update Documents

Mongodb uses the update method to update documents.

Update value of a field:
> db.persons.update({ "name": "Chad" }, { $set: { "name": "Anthony" } })

This updates the value of name field of the document from Chad to Anthony

Result:
> db.persons.find({ "name": "Anthony" })

{ "_id" : ObjectId("5084eaec3a54d35a3ce18ae6"), "name" : "Anthony", "salary" : 45000 }

comments powered by Disqus