lördag 27 juli 2013

Getting acquainted with MongoDB


This week I've spent some time checking out AngularJS - the popular Javascript framework for structuring single page web applications. I will not talk much about Angular in this blog post though, I'll get back to that later. The example app I made for learning Angular needed to store some data on the server-side, and for this I needed some database running on my server. I had already decided my server app would be a Node.js app, and so I thought this was a good opportunity to start looking into MongoDB, which seems to be a popular database format for web apps recently.

So this blog post is just a description of what I did to install and get acquainted with MongoDB, as preparation for making an app using AngularJS. And I'll get back to describe the app implementation in my next posting.

As a super short introduction, MongoDB is an open source database system which stores data in the JSON format, which makes it suitable for Javascript apps. And since you can now run Javascript on the server using Node.js, MongoDB seems to be a popular database choice for Node.js server apps.

To install MongoDB on my server, I followed these instructions:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

After installation, you can run Mongo as an executable, and create and edit databases, from the shell.

To start the executable, in the server shell I just wrote:
mongo

Then, I wanted to create a database named fridge. To do so, I wrote:
use fridge

I learnt that even though you do this, the database is actually not created on disc until you write some data to it. So I decided to put in some initial data.

I also learnt that Mongo has a concept of collections. If I understand correctly, a database can contain one or more collections. When you insert or retrieve data, you need to specify which collection you are working with. (Perhaps collections are similar to tables in the world of SQL?)

You don't need to create a collection explicitly. When you insert an element and specify which collection to put the element in, the collection will be created for you if it does not already exist.

So, as a test, I inserted two elements (or documents as Mongo seems to call it) like this:
db.products.insert( {product:"Tomatoes", quantity:"2"} )
db.products.insert( {product:"Eggs", quantity:"6"} )

  • db is a reference to the database you are currently working with.
  • products is the name of the collection you want to insert data to.
  • insert is the command for putting in data.
  • And within the parentheses you can see the JSON formatted data we are putting in. For my database I wanted two fields, "product" and "quantity" and as you can see I've put some tomatoes and eggs there. :-)

If you want to make sure you are working with the right database, you can write just db and Mongo will answer which is the active database. (In our example it should answer fridge.)

You can ask Mongo to list the collections available in the current database, like this:
show collections

And you can ask Mongo to list all the data in the collection called "products", like this:
db.products.find()

That should show you the tomatoes and eggs we inserted just before.

To exit Mongo, just type:
exit


As I wrote in the beginning, I wanted to use MongoDB from a Node.js app. So now that I had created a database, as a next step I installed a Node.js package for using Mongo from Node.js, like this:
npm install mongodb

And that's it for our preparations as of now. In the next posting I'll talk about how I put this to use.

Inga kommentarer: