4.1 Moving to MongoDB
In this lesson we can begin to move away from the default JSON file-backed database that we’ve been using so far and move to a more suitable production database. Let’s complete our MEAN stack by installing and configuring MongoDB.
1.Introduction3 lessons, 11:27
2.Sails.js From the Front-End2 lessons, 20:07
3.Sails Blueprints2 lessons, 13:34
4.Building the Back-End7 lessons, 58:40
5.Additional Techniques2 lessons, 09:55
6.Conclusion1 lesson, 01:28
4.1 Moving to MongoDB
Hi folks. In this lesson we're going to install and configure MongoDB, a popular document based database alternative to traditional relational databases like SQL. We've already got the rest of the MEAN stack. Express, which is one of the foundations upon which Sails is built. Angular, which we saw when we put the front end together. And Node of course, because without that, we'd be right back at the start of the course. So let's download Mongo, the M in MEAN, to complete the setup. We can get it from MongoDB.org and there's a big green button there, Download MongoDB. Let's hit that. And it we scroll down slightly, we can see that there are various platforms that are supported. There's Windows, Linux, Mac, OS X and Solaris. So, download either the installer or the package for whichever platform you're using. I've already got a copy downloaded and on my desktop ready, so I'm gonna run that now. So on Windows, there's one thing that we should do during the install process and that's make sure that Mongo gets installed to a location that doesn't have spaces in the path. By default, it gets installed to Program Files, which does have a space in it, so we need to click the Custom button in the Installer. So then we can click Browse and we can see path that it wants to install to but we'll need to change that. And instead we'll just go to the routes of C and we'll stick it there instead. And we can choose the same path that it wanted to install to previously. It's just going to the root of C instead of in Program Files. So we'll wait for that to install. It shouldn't take too long. And once that's installed we should probably add Mongo to our path so that we can run Mongo commands from anywhere on our system. So on Windows we can do this by going to the Control Panel. And then we want to go into System. And then we can hit the Advanced System Settings option at the left here. And Environment Variables. Then we want to find the path system variable, We need to make sure that we end any previous entries with a semi colon and then we can add the path that we just installed Mongo to but it needs to go to the bin folder. So once that's done, we should then be able to run Mongo commands from anywhere on our system. Let's give that a try. So the mongo shell startup, and it tells us the version and that is trying to connect to the test db. And we then get a message to say that it failed to connect. And the reason why it failed to connect is because the mongo server Is not actually running, so to stop the mongo server, we need to use the mongod or the mongod command instead and there's a little bit of set up that we need to do. As you can see, it's looking for a data directory at the root of the C drive, so let's just go and create that now. And that needs to be called data. Inside this folder we need to create another new folder called db, and this is where Mongo stores all its data. Okay, great. So now we should be able to start the server. And it's started and there's a message at the bottom saying that it's waiting for connections on the default port of 27017. So now we can open up another Command line. And this time we can run the Mongo command again. And this time, we don't see the error message because the server is up and running. So, let's have a look and see what collections are in the test DB. And we can see that our output's an empty array. There are no collections yet. So, let's create a new collection now as an example. And we do that using the create collection method. And we should get some feedback to say that the command finished okay. So now if we run the get collection names method once more, we should see our new collection. And we see an array, and this time we have our collection and also Mongo has created an indexes collection for us as well. We can insert a new record into our collection using the insert method. And again, we see some feedback and it looks like it went okay. So let's see if we can retrieve the record that we just created and we can do that using the find one method. And it returns the record that we just created. So if we just wanna get one of the fields inside the record then we can do that using dot notation. And this should just return the word, world, as it does. So I think we can safely agree that MongoDB is installed, and working as expected. So let's just remove our test collection. And we do that using the drop method. And it returns true to say that the collection was dropped. So if we run the get collection names method one more time. Then again we're back to having just the system.indexes collection that Mongo created and maintains automatically for us. So we don't need to know too much about the ins and outs of how Mongo works because sales will abstract that away for us behind water line, the built in ORM and ODM tool that handles interacting with different types of database for us. It's like a facade that we can query ourselves, and which then queries whichever database we're using for us. This allows us to interact with many different types of database using exactly the same code. So for the rest of the time that we're running the example, we just need to make sure that Mongold.exe is running otherwise none of our commands would work. So in this lesson we installed and setup MongoDB and looked at how to perform some basic actions such as creating collections and records and accessing those records. We also learned that Sales has a tool called Waterline, which abstracts away the differences between different types of database in order to keep our code streamlined. Because when we have to talk waterline and waterline entails with whichever database we're using. Thanks for watching.