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 keyserver.ubuntu.com –recv 7F0CEB10

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

Put the following content:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Save this file and continue these following commands:

$ sudo apt-get update

$ sudo apt-get install mongodb-10gen

=> Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux/

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
Or
$ 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:

> db.tours.insert({tour_name: “The Sweet Heart”})

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

> db.tours.find()
{ “_id” : ObjectId(“50217560d6238b650cf0d4fd”), “tour_name” : “The Sweet Heart” }

One more added:

> db.tours.save({tour_name: “The Honey Honey”})

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

> db.tours.count()
2
> db.tours.find()
{ “_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:

> db.tours.find({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:

> db.tours.update({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:

> db.tours.find({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:

> db.tours.update(
… {tour_name:”Love Forever”},
… {
… “$set”: {
… favorites: {
… movies:[“rocky”, “hiphop”]
… }
… }
… }
…)

Let’s modify “The Honey Honey” similarly:

> db.tours.update(
… {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:

> db.tours.find()
{ “_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:

> db.tours.find({“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:

> db.tours.remove({“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:

> db.tours.drop()

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

Gửi phản hồ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:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: