We have implemented MongoDB on a number of past projects and continue to, as we are a MongoDB partner. We wanted to share some of the knowledge we’ve gained and pass along tutorials on how you can get the most out of MongoDB with your current or future projects. Check out this easy to follow tutorial.
The D Word: Databases. Enter: MongoDB
In this tutorial, we’re going to talk about creating and managing a database for your project. Of course, we’re going to use MongoDB and I’ll tell you why. Obviously if you’ve been building for awhile you know there’s the SQL vs NOSQL debate. At some point in your life you’ve probably come in contact with a SQL database at somepoint in your career, and by default, each project on Meteor comes with a database. The database you’ll be using with Meteor is not a SQL database, but MongoDB.
There are a few differences between MongoDB and SQL databases, but they’re quite minimal. At this time there are no other databases you can use with Meteor, but that’s not going to be a problem for you for two reasons: you’re probably an easily adaptable human and MongoDB is a great database. So no need to worry, beginners. One important thing to remember is that the language is slightly different with MongoDB:
In SQL you would refer to a TABLE, MongoDB uses COLLECTION.
In SQL it’s ROW, in MongoDB it’s DOCUMENT.
In SQL you say COLUMN, in MongoDB you would call that FEILD.
That wasn’t too hard, right? DATABASE and PRIMARY KEY mean the same thing in both SQL and MongoDB.
Creating a Collection
To illustrate an example, let’s talk about Leaderboards. The main feature of all leaderboards and the original is a list of players, naturally, and a good place to start in terms of this tutorial. Without the names or players you can’t build anything so we’re essentially starting from the middle and building outwards to the nitty-gritty.
So the first step is to figure out where we’re going to store the data for each player and how are we going to display that information. The answer to the first question is: inside the database – which sounds like a horror movie but it’s far from that.
This is where you would be placing the players inside of your brand new leaderboard project in MongoDB. Name the new project whatever you want but remember that it will need to be unique, lest you receive and error from Meteor.
You’re chugging along but you still need to be able to reference your collection. Use this next line of code to do just that by placing your collection inside of a variable:
1PlayersList = new Mongo.Collection('players');
Don’t use the var keyboard, because you’re looking to create an aglobal variable, meaning you’ll be able to reference/manipulate it the collection. Next step is to save the file and switch to Google Chrome, and enter the name of your collection into the variable console:
If you do not receive an error you’re ready for the next step. If you did receive an error, check your spelling.1 PlayersList
You have three options when it comes to inserting data:
Through a form in the interface
We’re going to use the first version since it’s the easiest.
Inside the Console, write the following:
The next step is to pass JSON-formatted data between the brackets, and this data will then be inserted into the collection. If you’re not familiar with the JSON format, look at this:
2 3 4
2 3 4
Here you have a complete insert function, and when you hit the return key in the console, a document will create inside the Players List collection. You would use this function to create however many players or names you want in your leaderboard.
Now that you have a collection featuring Canadian national treasure and greatest hockey player of all time we should now learn how to find him if need be. So, through the console let’s locate Wayne.
And there you have it! This is just the tip of the iceberg in terms of the many things you can do with MongoDB and Meteor.JS. Check back periodically for more easy-to-follow tutorials.