IT Staff

Blog về chuyên ngành IT

What is MongoDB?

leave a comment »

* What is MongoDB?

MongoDB is a DBMS for web & internet infrastructure.
+ new data stores replacements for relational database systems.
+ has based its query language on JSON
+ Key-value stores are extremely fast and relatively easy to scale.
+ its data format: BSON
+ real-time analytics data
+ dispense with memcached.
+ support secondary indexes, dynamic queries
+ easily stores schemaless data (schema free document), and also good for capturing data.
+ auto sharding, auto failover handled, support replication, scale horizontally.

* Install MongoDB on Ubuntu

– You can use apt-get to install MongoDB v2.0.6 package:

$ sudo apt-key adv –keyserver –recv 7F0CEB10

$ vi /etc/apt/sources.list.d/10gen.list

Put the following content:

deb dist 10gen

Save this file and continue these following commands:

$ sudo apt-get update

$ sudo apt-get install mongodb-10gen

=> Ref:

Or using Synaptic Package Manager, in Quick search, type ‘mongo’ to search the available mongodb package for ubuntu (current available version is 1.2.2).
Check this package then click Apply button to install that package in your system.

– Make sure that mongod is running:

> ps aux | grep mongod

mongodb 1018 0.0 0.1 162720 4188 ? Ssl 02:14 0:00 /usr/lib/mongodb/mongod –config /etc/mongodb.conf
rrrubbby 2577 0.0 0.1 24376 4404 pts/0 Sl+ 03:05 0:00 /usr/lib/mongodb/mongo

* Starting the MongoDB shell

$ sudo service mongodb start
$ sudo /etc/init.d/mongodb start

** Execute the mongo:

$ mongo
MongoDB shell version: 2.0.6
url: test
connecting to: test
type “exit” to exit
type “help” for help

** Create a database

I like the honey moon days – I think you too :), so it’s named is: honey_moon.

> use honey_moon
switched to db honey_moon

MongoDB doesn’t require creating the database. Databases and collections are created only when documents are first inserted.

Now, we insert the first simplest document.

** Insert the document

The document in this sample’s named ‘tours’ and it’s content may be liked this:

{tour_name: “The Sweet Heart”}

And here we go:

>{tour_name: “The Sweet Heart”})

If the insert succeeds, you can show that saved document by using find method:

{ “_id” : ObjectId(“50217560d6238b650cf0d4fd”), “tour_name” : “The Sweet Heart” }

One more added:

>{tour_name: “The Honey Honey”})

Then using count method to get amount of rows in the table:

{ “_id” : ObjectId(“502402488fbd773a00b88f39”), “tour_name” : “The Sweet Heart” }
{ “_id” : ObjectId(“502402728fbd773a00b88f3a”), “tour_name” : “The Honey Honey” }

Tips: We can use a query selector to the find method, like this:

>{tour_name: “The Sweet Heart”})
{ “_id” : ObjectId(“502402488fbd773a00b88f39”), “tour_name” : “The Sweet Heart” }

Next, we will take a loot at how to update the data.

** Update documents

The update method require two arguments at least. The first specifies which documents to update and the second tells how to get the documents should be modified. Take an example for this:

>{tour_name: “The Sweet Heart”},{$set: {tour_name: “Love Forever”}})

This will change the tour which has name “The Sweet Heart” to “Love Forever”. Check out it:

>{tour_name: “Love Forever”})
{ “_id” : ObjectId(“502402488fbd773a00b88f39”), “tour_name” : “Love Forever” }

Tips: if you want to your table is complexer, may be like this:

{ tour_name: “Love Forever”,
favorites: {
movies: [“rocky”, “hiphop”]

=> the favorites symbol is contains the movive sybol. And we will update only this new schema and data in the tours table which has the name is “Love Forever”. Ok, let go with this modifier:

… {tour_name:”Love Forever”},
… {
… “$set”: {
… favorites: {
… movies:[“rocky”, “hiphop”]
… }
… }
… }

Let’s modify “The Honey Honey” similarly:

… {tour_name:”The Honey Honey”},
… {
… “$set”: {
… favorites: {
… movies:[“Small Wonder”, “hiphop”]
… },
… places: [“Nha Trang”, “Sa Pa”, “Da Lat”]
… }
… }

Now, show the tours collection to make sure all updates succeeded:

{ “_id” : ObjectId(“502402488fbd773a00b88f39”), “favorites” : { “movies” : [ “rocky”, “hiphop” ] }, “tour_name” : “Love Forever” }
{ “_id” : ObjectId(“502402728fbd773a00b88f3a”), “favorites” : { “movies” : [ “rocky”, “hiphop” ] }, “places” : [ “Nha Trang”, “Sa Pa”, “Da Lat” ], “tour_name” : “The Honey Honey” }

Bravo, it’s good. So, if I want to find a tour has the movie “rocky”, so I can do:

>{“favorites.movies”: “Small Wonder”})
{ “_id” : ObjectId(“502402728fbd773a00b88f3a”), “favorites” : { “movies” : [ “Small Wonder”, “hiphop” ] }, “places” : [ “Nha Trang”, “Sa Pa”, “Da Lat” ], “tour_name” : “The Honey Honey” }

Tips: You can practice update method with operators: $set, $addToSet and $push.

Okay, now we should to move on removing data.

** Deleting the data

The last is removing data is good to be known after creating, reading, and updating data.

Without parameters, the remove operation will clear all the row of the document.

With a woohoo collection, you can do like this:

> db.woohoo.remove()

Or with the specify expression:

>{“favorites.places”: “Nha Trang”})
This will remove all tours which favorite place is Nha Trang.

Note: remove() does not delete the collection, it’s just remove documents from a collection.

You can use the drop method to delete the collection:


Okay, now you can practice yourself for inserting, updating, deleting or indexing more in MongoDB Javascript Shell 😉
All of this practice, I get from the MongoDB-in-Action book, so you can get and enjoy it.

Any comments to share your experiences is welcome!
Thanks for your attending and see you in my next post 🙂

Written by Xavier

Tháng Tám 3, 2013 lúc 9:53 sáng

Posted in Staff

Tagged with

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất /  Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )

Connecting to %s

%d bloggers like this: