Setting up MongoDB - AdarshMaurya/mongodb-and-mogoose-for-node-getting-started GitHub Wiki
Introduction
Hello, and welcome to this module on setting up MongoDB. This module introduces you to some of the various MongoDB hosting options that are available, both cloud based and local hosting, and what will be used throughout this course. MongoDB itself will not be deeply discussed as this is covered in other courses. Lastly, we will up our local MongoDB database and collections, as well as import some seed or sample data that will be useful as we develop our demo application together. Let's get started on that now.
MongoDB Hosting Options
MongoDB is supported on a wide variety of platforms and operating systems including Linux, macOS X, and Windows, so you'll find a wide variety of hosting options to choose from. MongoDB was developed to run just fine on most commercially available computer systems and scales extremely well on these systems. So again, your options are pretty open. One thing to make note of is that beginning with version 3.4, MongoDB no longer supports 32-bit x86 platforms. So you'll want to make sure that you're running a 64-bit system. Whether you choose to eventually host your database in the cloud or not, it's still a good idea to download and install MongoDB on your local development box. Simply go to mongodb.com and click on the green Get MongoDB button and select the platform and operating system that's right for you. Installation is straightforward, quick, and pretty easy.
Another option to consider is to set up a virtual machine using a service such as Microsoft Azure. The process of downloading MongoDB and installing it will be the same as if you were doing it on a local desktop or laptop, but you do not have to purchase the physical hardware, and you don't have to set up the operating system. That's already done for you.
Cloud-based virtual servers can be a very cost-effective option, but in most cases, you will still be responsible for maintenance and upkeep of the VM, such as keeping it patched and up to date. You will also be responsible for setting up the configuration and security for your MongoDB servers.
Personally, I believe a better option is to use a database as a service or a cloud-based hosting offering, such as mLab. A couple of other really good choices to consider are MongoDB Atlas and Compose. There are certainly other cloud-based hosting offerings to consider, so do your research and pick the one that best suits you and your business needs and budget. Some services offer a free Sandbox account. The one we've selected for this project and demo application is mLab. They offer a Sandbox account with half a gig of data allowed on each, and that should be more than enough space to get our project developed, tested, and ready for production. That being said, by the time you watch this course, mLab may no longer be a valid option for you. During the development and recording of this course, mLab was purchased by MongoDB, and all accounts will eventually be migrated over to Atlas.
The Virtual Standups Database
For our demo application, we'll start with a local instance of MongoDB, and then towards the end of the course, migrate that to a hosted service in the cloud. For this course, we'll end up migrating our local MongoDB database to a cloud-based Sandbox account on mLab, but you should absolutely explore the other hosting options mentioned earlier, especially Atlas; however, if you're following along, feel free to use whichever cloud hosting service you feel the most comfortable with. It's time to build the virtual standup database locally to start with now. And as mentioned in the previous clip, if you do not yet have MongoDB installed locally for your particular operating system, this might be a good time to pause the video and go to the MongoDB website at the URL shown here and download and install the version appropriate to you. MongoDB has already been installed locally on this computer. So we're opening up a Command Prompt here, and I will be using Windows 10. We need to make sure that our local instance of MongoDB is up and running. We'll do that by entering mongod here. Now that we can see that MongoDB is up and running, let's take a look at our local MongoDB instance using Studio 3T. Once that is open, we'll want to get connected to our locally running database. In Studio 3T, you should notice a green Connect button. Click on that. There's also a Connect button on the top menu bar, and optionally, you can go to the File menu and select Connect there too. We should have our local MongoDB database running, and the name is local. Select that and press Connect. Now that we're connected, let's add a new database that we'll name virtualstandups. With this new database, we'll need at least three collections, standups, which will be used to store our virtual standup meeting notes; projects, which will be used to obtain a list of projects for the drop-down list we talked about in the course introduction module; and likewise, teammembers, which will be the list of active team members for whom the note entry is for. To add a new collection, we can simply right-click on the collections folder and select Add Collection, and this first one we'll name standups. Make note of the options here, but for our purposes, we can leave these as they are. Next, let's add collections for projects and team members. So that we have some sample data to work with during development, let's import some seed data into these three collections now. For example, to import team members, we select that collection, and one option is to click the Import button on the top menu bar. The file we want to import is in JSON format, so the default selection here is fine, next, click the green plus in the upper left here to add source files, and we'll start with the aforementioned teammembers.json file. The JSON files I wish to import are located here, but if you download the demo files, yours may be in a different location. Pressing Next, we get a preview of the import, Next again, a quick import summary, and then Start Import to bring that data into our selected collection. Repeat that process again for project and standups. I'll do that quickly here as well. Now that we have some sample data imported to work with, let's open up the standup collection of documents and examine a sample document and its structure. This document is pretty straightforward and simple, but it stores exactly what we need in our virtual standup meeting notes. You may notice that the order of the fields show here are not exactly the same order as what we saw in the completed demo. This doesn't really affect the document in any way. This is simply a sample document to illustrate what we will be building a Mongoose schema against. That's not to suggest that we are taking a database-first approach here. This is merely a peek at a document that we will end up creating through Mongoose in our demo application. With MongoDB and Mongoose, there is nothing restricting us from changing our minds and adding a document to this collection with a completely different structure in the future. So long as the application, which is our Express API server in this particular case, knows how to handle those changes, MongoDB, or our filing cabinet if you will, doesn't care what we put in there. We as the developer just need to know how to get our application data back out and work with it in whatever form we find it in. Before we leave this clip, notice that Studio 3T includes a Query Builder where you can manually build and test queries out or use the Virtual Query Builder as shown here. Also, make note of the Query Code tab near the Results tab. Here you'll find examples of exactly what is going on in the Mongo shell or select examples for other languages, such as JavaScript, Node.js as well. Another helpful feature of Studio 3T is that we can edit documents directly in a graphically friendly way. Let's take a look at how to do that right now. Say for example, if we wanted to change a document in the projects collection say from being active to inactive so that it no longer displays in the drop-down list. You would simply edit that here, setting the value to false and then perhaps validate it to make sure that the JSON format remains valid, and then save the document. It's that simple. One last tool that we will take a real quick look at is the predecessor to Robomongo, now called Robo 3t. Opening that up and getting connected, you can see that it's similar to, but not as full featured as Studio 3T. It's something to consider using if you have used Robomongo in the past or need a free and lightweight tool to work with your MongoDB database and do not care to solely rely on the command-line interface. Well, that's a quick look at our local MongoDB database and sample documents that we'll use as we continue to build our demo application together, as well as how we can use Studio 3T, or Robo 3T, to gain access to that data in a graphically friendly and useful way.
Next Steps
In the last clip of this module, let's consider some next steps to take. As mentioned earlier in this module, if you do not yet have MongoDB installed locally on your computer and if you want to follow along, this might be a good time to consider getting that installed. Once again, here is a look at the mongodb.com website where you can go and download the version appropriate for your operating system and platform. Towards the end of the course and as noted before, we'll be migrating our local MongoDB database to the cloud.
If you want to follow along with that process, this might be a good time to go ahead and get a Sandbox account set up. And as a quick reminder, here were a few good choices to consider and the websites to go to to find more information on each. It's certainly not required, but we'll be using mLab for this course. But as mentioned earlier, mLab will within the next several months be migrating accounts to MongoDB Atlas, so please do keep that in mind.