Hey! I am Swaroop SM. I enjoy writing code. Read more...
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:
$ 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
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.
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"]
})
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.
> db.persons.find()
{ "_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" ] }
> db.persons.find({"name": "Swaroop SM"})
This retreives all documents with the name field having the value Swaroop SM.
{ "_id" : ObjectId("5084dbc63a54d35a3ce18ae0"), "name" : "Swaroop SM", "gender" : "Male", "website" : "swaroopsm.github.com", "friends" : [ "Joe", "Chris", "Kathy", "Thomas" ] }
> db.persons.find({"name":"Swaroop SM"},{"friends":1})
This retreives all the friends of Swaroop SM.
{ "_id" : ObjectId("5084dbc63a54d35a3ce18ae0"), "friends" : [ "Joe", "Chris", "Kathy", "Thomas" ] }
> db.persons.find({"name": "Carl James"},{"friends.website":1})
This retreives a all the websites of Carl James’ friends.
{ "_id" : ObjectId("5084e49d3a54d35a3ce18ae2"), "friends" : [ { "website" : "joe.abc.com" }, { "website" : "chris.xyz.com" }, { "website" : "kathy.pqr.org" }, { "website": "thomas.ttt.in" } ] }
> 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.
> db.persons.update({ "name": "Chad" }, { $set: { "name": "Anthony" } })
This updates the value of name field of the document from Chad to Anthony
> db.persons.find({ "name": "Anthony" })
{ "_id" : ObjectId("5084eaec3a54d35a3ce18ae6"), "name" : "Anthony", "salary" : 45000 }