I'm a big fan of CouchDB. I enjoy how they go about doing things and how you are able to use it without the need of drivers as its all RESTful based. As long as your language of choice has the ability to make RESTful calls and read JSON data then you're all set. But before I really started to use CouchDB I did start looking at MongoDB, another Document data store. MongoDB is now being compared to mySQL as far as its use by projects. It has a great group of developers along with getting some great press from big companies moving over to MongoDB from some type of SQL based system.
I wanted to take another look at MongoDB and eventually do some playing around with it in Ruby for another posting.
Installing
You can get MongoDB from their download page at http://www.mongodb.org/downloads, for MacOS, I installed it the same way for these types of apps.
1 2 3 4 5 6 | |
Don't forget the last part, /data/db is the path where your databases are saved. After moving around the files be sure to add lines below to your .profile in your home folder.
1 2 | |
Now lets test it out. First we need to start the server and then run the clients for it.
1 2 3 4 5 6 7 | |
and now in a new shell start the client,
1 2 3 4 | |
At this point you are all set to start playing around with the database. There are also downloads for Linux, Windows and Solaris along with packages for Ubuntu and Fedora.
Some playing around
Now that we have everything all set, lets play around in the client. This is a Javascript base client and documents are JSON data, so if you have worked with JSON before you should be familiar. Sadly there is no web interface like the one that comes with CouchDB, but you can get a 3rd party one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
So what's happening here?
The first line, 'connecting to: test' tells you that you have connected to the test database. The use playingaround is switching/making a new database for you. As far as the 'mytest', this is a collection, think of a collection like a table, it's what is going to hold the documents for you.
The next lines are making a document and saving it into a doc variable to be used later. As you can see it's JSON format, to save this in to the collection all you need to do is run db.mytest.save(doc), where you would replace 'mytest' with your collection name.
The .find() and .findOne() will show you documents that are in the collection, .findOne will only show you one document at a time though you are able to pass in criteria in to the find like I did with the .findOne({fname:"Dave"})
This is a very quick overview of using the client, you can find much better info on MongoDB.org's Tutorial and MongoDB.org's Overview
If you have any questions or comments please post, also any suggestions on improving this are welcome.