Quantcast
Channel: Jasko Koyn » learn
Viewing all articles
Browse latest Browse all 10

MongoDB – Node.js Tutorial for Beginners

$
0
0

In this tutorial, we’ll be talking about MongoDB and how we can use it to store our data. MongoDB will provide us with a simple CRUD (Create, Retrieve, Update, Delete) API.

Using MongoDB

Node.js and MongoDB are 2 completely different things. MongoDB is a database. So, it isn’t installed with Node.js. Node.js also doesn’t support MongoDB out of the box. Luckily, there are a lot of community created modules that will make connecting to a MongoDB database easily.

If you’re new to MongoDB, then don’t worry. We’ll go over the basics. We have to install MongoDB before we get started, but we actually won’t be going down that road. Instead, we’ll use an online service that provides us with a free MongoDB database. Go to MongoHQ and create a free account. MongoHQ will allow us to create a free database for development purposes. You’re limited to 512mb, but that will be just fine for our blog application.

Databases, Collections and Documents

Let’s define these 3 terms in MongoDB. A database is where all your data will be stored. You can think of it as a hard drive. For the most part, you’ll have only 1 database for each web application you create. Having multiple databases can be quite challenging.

A document is a JSON object that will store our data. Documents are very flexible in terms of how much and what kind of data you can store. You can think of a document as a file on our hard drive.

Of course, you have to be able to categorize your documents. For example, we can have multiple documents related to blog posts. That’s where collections come in. Collections are just categories for documents. You can think of it as a folder for files. We can have a collection called Posts that will contain our documents related to blog posts. You can already imagine how this database will be set up. We’ll have a collection for blog posts and another for users.

Let’s create our first database. In MongoHQ, click Add a Database and name this database blog.You should be taken to your blog database. Before we can access our blog database, we need to add a user first. Add a user and make sure you remember the username and password. A user for a database isn’t the same as a user for the website. A user for the database is basically an account that will have access to your database that can manipulate it anyway they want outside of MongoHQ.

Go to the collections page and create a collection named posts. Look at the image below for reference.

MongoDB

MongoDB

Let’s add some documents to our posts collection. You’ll be shown a JSON object with a property named _id. The _id property is a way to identify each document uniquely. This property is automatically created for you each time you create a document. However, the _id property is completely optional. We’ll be using the _id property always. Create 2 properties named title and content. It should look something similar to the image below.

MongoDB

MongoDB

Do this one more time and you should now have 2 blog posts created manually. I use lorem ipsum to create dummy content. You can check out it by clicking here.

Mini Exercise

Create a collection named users. We’ll be using our blog application to create users for us.

Conclusion

You learned a little about MongoDB, but we haven’t used it to its full potential. In the next tutorial, we’ll learn how to start displaying blog posts on our home page. You can learn more about MongoDB by clicking here.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images