Understanding Databases in MeteorJS

0
1472
Understanding Meteor JS Databases

Understanding Meteor JS Databases

If you’ve been working with Java and JavaScript for any amount of time, you’ve probably used or heard of Meteor JS. As a framework, it’s built on top of Node JS, which means that it’s able to serve as a development tool for both front end and back end projects. Iit’s also useful in that it’s able to develop projects that are intended for cross browser or cross platform use. A pretty common way of introducing Meteor JS in technical guides, blogs, and how to articles about using Meteor JS or other aspects of frameworks built on top of Node JS, is to start with front end development.

backend frontend

In some ways, this makes a lot of sense. It’s an obvious place to start for beginners, because front end user directed content is what they’re used to seeing a lot of the time as users themselves. It’s also a whole lot flashier than back end development, and it’s kind of where people show off their coding skills in order to come up with new cool things for websites. Additionally, learning some front end development has much quicker results, as progress working on the front end often leads to projects and development pretty quickly.

However, there are a couple of problems with this approach. First, front end development really builds off of back end development. Sure, there are plenty of things you can learn to do without any particular or deep knowledge of the back end but, especially where Meteor JS and other frameworks built on top of Node are concerned, understanding the back end is very important to being a competent front end developer. Also, although it might make sense to learn front end stuff first from a standpoint of wanting to display quick progress, it’s actually very counter-intuitive in a pedagogical sense to start with front end development. Since so much of it is based off the back end, learning the front end stuff first will mean a great amount of backtracking through material in order to re-learn concepts as they relate to both front end and back end development.

Although it isn’t very flashy and it won’t appeal to anyone’s sense of vanity, learning the back end of Meteor JS really is very important, and that’s the focus of this article. In particular, we’re going to talk about a few topics relating to databases, which are a key feature of back end development, and something that’s really taken for granted most of the time when developing on the front end. In particular, we’re going to discuss the particular data basing tools involved with Meteor JS, as well as a few simple (but fundamental and critical) functions that you can perform with the back end that will make front end development that much easier.

Meet the MongoDB database
mongodb
If you’ve been working on front end development, and in particular the kind of light development available through languages like PHP or services like WordPress, then you’ve been using an SQL database. SQL stands for Structured Query Language, and SQL databases are often used for special purpose programs and that kind of thing. They’re pretty easy to use, and the tools that they involve are fairly simple, as well. Unfortunately, something you’ll become quickly become aware of when you start using Meteor JS, is that Meteor JS doesn’t support SQL. In fact, it doesn’t support any kind of database except for its built in database, MongoDB.

The good news here is that MongoDB is actually very similar to SQL in its structure, and it’s also very easy to use. Many of the same concepts used in SQL are the same in MongoDB – they’ll just have a different name. Terms like table, row, and column that you’re likely familiar with from SQL are replaced by terms that mean the same thing in MongoDB such as collection, document, and field, respectively. Learning these new terms for old concepts that you already know can actually be harder to learn starting fresh out, but once you get the hang of it, you’ll adapt quickly to a new framework of terms and names for concepts you already know how to deal with.

There are other differences between SQL databases and MongoDB databases besides just naming conventions, but a lot of those things are pretty high level concepts that don’t really make any difference, and especially not to a beginning user. Now that we’re familiar with the kind of database we’ll be using, let’s go through a couple simple tasks that you’ll be able to do which will require a basic understanding of back end development and database work in general.

https://blogeduonix-2f3a.kxcdn.com/wp-content/uploads/2015/10/Learn-Javascript-And-JQuery-From-Scratch.png

Creating collections
One of the most basic functions you can do with a database is to create a collection. If you’ll recall, a collection means the same thing in MongoDB as a table does in SQL. When you’re setting up a collection, there are two basic questions to ask: where will we store the data associated with each item?

And how will we display the data associated with each item from within the collection interface?

Obviously, there isn’t really a single right answer to this question, but they’re important nonetheless.

One of the biggest challenges of database work, is to conceptualize a database that suits the particular work or task of the information it has to store, and that can be tougher than any kind of coding challenge. Once you’ve created a collection and you’ve set up how it will store and display data, you’re ready to move onto the next step, which is to actually enter information into it.

Insert data
You might think that entering data into a collection or other database might be the easiest part, but there are actually a few things to think about here. There are, in general, four ways to insert data.

  • First, you can insert it through the Javascript console.
  • Second, through the command line.
  • Third, through a Javascript file.
  • And, fourth, through a form available on the interface for the collection itself.

Of these options, using the Javascript console is probably the easiest, but it does involve mucking around with code, and it’s not really the most elegant solution. Of the four, the one that takes the most initial work is to create an interface on the collection itself. This might seem very preferable, but it’s important to remember that you’ll have to generalize the parameters, and make a special case for anything that falls outside of it. Once you’ve entered your data, you’ll want a way to access it, which brings us to our final point.

Locate data
Locate data
Once again, there are a lot of ways to do this and, once again, the simplest is to use the console. The command [collection name].find(); keys off the find function and serve as a general search tool. There are, of course, other ways to do this, namely to code a specialized search function into the collection, or to create a catalog of items in the database that links between the list and the database itself. Whatever option you pick, the important thing to remember is that your collection is organized by a set of parameters that make sense and function well.

Back end development can be intimidating especially if, like many others, you started by learning a lot of front-end stuff. Hopefully, this guide has given you an idea about databases, how they work, and how they relate to the larger project of back and front-end relationships in Meteor JS and other Node JS-based frameworks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here