Chris Hannah

Getting Started with MongoDB - Basic Messaging

After talking to a friend at university about wanting to start a little side project while I don’t have any coursework, he suggested I tried out MongoDB. He gave me a little demo just in Terminal and how easy it was to simply create a database, I never plan on using MySQL again after seeing this.

To create a new database all you need to enter is use testdb If it’s already been created then it will simply select that database, if not then it will just create it. Easy!

Then to create a collection inside a database (tables in MySQL), just insert some data! So for example if I entered this:

db.users.insert({name: “Chris Hannah”, username: “chris”, email: “chris@email.com”})

It would then create a new collection with the name users, and then insert a new document (row), with the corresponding values for name, username, and email.

Making it into something

So after I realised I could get the hang of MongoDB, I started seeing how I could use it in something like PHP. It took ages to get the different extensions installed, and also working with XAMPP (I wont discuss the trouble I had, it would take too long).

But after getting that sorted, I worked out a little code to display all the entries in the Users collection. That felt pretty nice to have, as I was still inserting the data via Terminal and then refreshing the page to see the data suddenly appear.

Mongo

After that I thought this could be useful for a basic messaging service, so I started a new messages collection in the database. The only data I stored was the user (which I will then correlate with the users collection eventually), the message text, and a timestamp.

I got the messages appearing just like I did with the Users. But after adding a basic form, and another bit of PHP to take the data and insert it into the database, I got something that slightly resembles a message board.

I’m now going to work on it a bit further, and it may develop into a basic service like Twitter, or a chat room thing, either way I’m not too bothered. It’s all for the purpose of learning, so just making something is good enough.

#Development

Reply via Email