MongoDB is a highly scalable, high-performance, open-source, document-oriented database. It’s a part of the NoSQL family of database systems, which means it doesn’t rely on the traditional table-based relational database structure. Instead, it employs a more flexible, document-oriented model. This flexibility makes MongoDB an excellent choice for projects that require seamless data integration and speed.

Understanding MongoDB

Before we dive into the setup process, it’s crucial to understand the fundamental concepts of MongoDB:

  • No Fixed Schema: Unlike SQL databases, MongoDB doesn’t require a fixed table schema. This means there’s no need to predefine columns and structure, providing you with the flexibility to store documents of different structures in the same collection.
  • Collections and Documents: In MongoDB, data is stored in collections, which are similar to tables in SQL databases. The individual items in these collections are referred to as “documents”, not “rows”. A document can contain many different key-value pairs, and MongoDB stores these documents in a binary representation called BSON (Binary JSON).
  • Scalability and Performance: MongoDB is designed to be scalable and boasts automatic sharding and full index support for high performance. It can be used to store large amounts of data without compromising the speed of read and write operations.

Prerequisites for Installation

Before setting up MongoDB, there are two essential tools you need to install:

  1. MongoDB: Visit the official MongoDB website to download the version suitable for your system. To determine the correct version, check your system architecture by typing wmic os get osarchitecture in the Command Prompt (CMD).
  2. RoboMongo: This is a shell-centric, cross-platform MongoDB management tool. It allows you to manage data and collections of data easily. Download it from the RoboMongo website.

Setting Up MongoDB

Once you’ve installed MongoDB and RoboMongo, you can proceed to set up MongoDB:

  1. Set Environment Variable: Navigate to the path where MongoDB is installed (e.g., C:\\Program Files\\MongoDB\\Server\\3.0\\bin for Windows) and set that path to the Environment variable in your system settings. This allows you to access MongoDB from any location in the command prompt.
  2. Create a Data Directory: MongoDB needs a directory to store its data. Create a new folder in your system for this purpose. For example, you can create a folder named mongodata in the C drive.
  3. Connect MongoDB to the Data Directory: To connect MongoDB to the data directory, use the command mongod.exe --dbpath "c:\\mongodata". If you’ve set the environment variable, you can simply type mongod --dbpath "c:\\mongodata". This command connects your database to port 27017.

Please note that the above setup process is for Windows. If you’re using another operating system like OS X or Linux, you can find the corresponding setup instructions on the MongoDB website.

Creating a Database for a Bookstore Application

Now that MongoDB is set up, let’s create a database for a bookstore application:

  1. Start MongoDB: Start MongoDB and ensure it’s running on port 27017.
  2. Access MongoDB: Open another command prompt and navigate to the path where MongoDB is installed. For Windows, this path is typically C:\\Program Files\\MongoDB\\Server\\3.0\\bin. To access MongoDB, type the command mongo.
  3. Create a Database: To create a database named books, use the command use books. This command is used to create or switch databases.

With the above command, we have successfully created a database for our bookstore application. We will use this database in the API section, where we have established a connection to the database in the server.js file (i.e., var db = mongo.db("mongodb://localhost:27017/books", {native_parser:true})) while creating APIs.

Finally, you can run your application. For a full tutorial on creating APIs and running the application, you can refer to this full tutorial.

This tutorial is part of a larger MEAN Stack series, which covers various aspects of MongoDB, Express.js, Angular.js, and Node.js development. By following this series, you’ll gain a comprehensive understanding of MEAN Stack development, from setting up the necessary tools to creating robust, scalable applications.

Similar Posts