Monday, October 25, 2010

Playing with MongoDB – Part One

With all this hype about NoSQL databases and a fact that products like Cassandra, MongoDB, RavenDB, CouchDB are getting more and more attention between us, traditionally (RDBMS) oriented .NET developers :), I feel strong need to become a part of this new stream (perversion?) and of course I want to play with a new toys.So, let’s get started. I’ll omit all the NoSQL definitions, products’ comparisons and other yada-yadas – feel free to use Wikipedia and Google to get all you need. Ok, just a few points to help you get started:


Goals

  • Set up MongoDB environment using Windows Server2008R2(Windows7) and Ubuntu 10.04
  • Configure sharding
  • Evaluate existing .NET APIs using Windows/.NET4/VisualStudio2010
  • Evaluate existing .NET APIs using Ubuntu/Mono2.6/MonoDevelop
  • Other stuff

logo-mongodb

Set up Distributed MongoDB Environment

We Will Need:
  • Windows box. I’m using Windows Server 2008R – so all the settings/configuration are guaranteed to work only on Windows2008R2 or Windows7
  • Linux box. Again, the instructions are guaranteed to work only for Ubuntu 10.04
  • Working internet connection
  • Spare time [(-_-)]

Installing MongoDB on Ubuntu 10.04

First, setup MongoDB repository to get latest (stable) product versions.Open repository list file for edit,
~$ sudo nano /etc/apt/sources.list
add new line with the MongoDB repository spec,
deb http://downloads.mongodb.org/distros/ubuntu 10.4 10gen
save and exit nano editor. To be able to install a package from a mongoDB repository we will install appropriate key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
And now the package itself (stable version)
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install mongodb-stable
Yepp! That’s really SO simple.Now you could control MongoDB service using
sudo service mongodb start
and/or
sudo service mongodb stop
By default, MongoDB service listens on a 27017 port, serves administration console on http://localhost:28017 and stores all the configuration in /etc/mongodb.conf file.

Installing MongoDB on Windows Server 2008R2/Windows 7

Use your favorite browser to download appropriate MongoDB windows binaries from here. You could start using mongoDB just after unzipping the package, but let’s make things right. Windows zip package doesn’t include any all-in-one installer like ubuntu/debian package does – we will need to set up everything manually.Unzip the package into any nifty location like  C:/mongodb-1.6.3. You should get something likeimage Now let’s create logs and data folders to store appropriate stuff. Also, I’ll create mongodb.conf configuration file here. image Edit configuration file as
logpath = C:\mongodb-1.6.3\logs\service.log
logappend = true
dbpath = C:\mongodb-1.6.3\data\
directoryperdb = true
To install MongoDB as Windows service, bring up command line as Administrator and change current directory to MongoDB “installation” folderimageexecute installation command with correct –-config parameter value
bin\mongod --config "C:\mongodb-1.6.3\mongodb.conf" --install
This will install mongodb as a Windows service. Now we are ready to start MongoDB service
image
check log file, should be something like
Mon Oct 25 13:44:04 MongoDB starting : pid=5880 port=27017 dbpath=C:\mongodb-1.6.3\data\ 64-bit 
Mon Oct 25 13:44:04 db version v1.6.3, pdfile version 4.5
Mon Oct 25 13:44:04 git version: 278bd2ac2f2efbee556f32c13c1b6803224d1c01
Mon Oct 25 13:44:04 sys info: windows (6, 1, 7600, 2, '') BOOST_LIB_VERSION=1_42
Mon Oct 25 13:44:04 [initandlisten] waiting for connections on port 27017
Mon Oct 25 13:44:04 [websvr] web admin interface listening on port 28017
Voila! However, it’s weird, how many frictions were needed to install MongoDB on Windows compared to Ubuntu.

Verifying MongoDB Installation

Start MongoDB service and bring up mongo console (looks like it’s javascript based). You should see something like
~$ mongo
MongoDB shell version: 1.6.3
connecting to: test
>
now let’s create our own DB
> use myMongo
switched to db myMongo
>
MongoDB uses “lazy create” approach – so it will allow you to switch to myMongo DB even if  it does not exist yet, engine will create it later, after the first DB operation. The same picture with other DB structures. Let’s proceed with CRUD.
Create (Insert):
> db.customers.insert({FirstName:"John", LastName:"Doe"});
>
db.customers collection was “lazy created”.
Read (Find):
> db.customers.find({"FirstName":"John"});
{ "_id" : ObjectId("4cc4c3765eb8733a6b41dcfd"), "FirstName" : "John", "LastName" : "Doe" }
>
Update:
> db.customers.findAndModify({update: {"FirstName":"Joe", "LastName":"Dohn"}, query: {"FirstName":"John"}});
{
"_id" : ObjectId("4cc4c3765eb8733a6b41dcfd"),
"FirstName" : "John",
"LastName" : "Doe"
}
>
Now, when we select all records we should notice change:
> db.customers.find();
"_id" : ObjectId("4cc4c3765eb8733a6b41dcfd"), "FirstName" : "Joe", "LastName" : "Dohn" }
>
hm, worked (-_-)

Delete:
> db.customers.remove({"_id":ObjectId("4cc4c3765eb8733a6b41dcfd")});
> db.customers.find();
>
as you can see worked again (for me).  In a next post I’ll try to make something useful with our boxes – will configure and play with Data Sharding.

No comments:

Post a Comment