Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / Javascript
Tip/Trick

MEAN Stack Development with MongoDB

Rate me:
Please Sign up or sign in to vote.
3.83/5 (9 votes)
1 Feb 2015CPOL6 min read 36.5K   13   23
This is an Introduction tutorial about the MEAN stack and MongoDB

Introduction

  • This is just an introduction of MEAN Stack and MongoDB.
  • I'll write more about MEAN Stack in the future.
  • This is the first part of the series of articles which I will write about the MEAN.

Image 1

What is MEAN Stack ?

  • The MEAN stack is a powerful, full-stack JavaScript solution.
  • That contains 4 major building blocks or components.
  • Which are MongoDB, Express, AngularJS and Node.js

What is MongoDB ?

Image 2

  • MongoDB is an open source, document-oriented database.
  • In MongoDB you can store JSON-like documents with dynamic schemas.

What is Express ?

Image 3

  • Express is a minimal and flexible Node.js web application framework.
  • It provides a robust set of features for building single and multi-page, and hybrid web applications.

What is AngularJs ?

Image 4

  • AngularJS is a web client framework.
  • It extends the HTML vocabulary for your application.
  • The resulting environment is extraordinarily expressive, readable, and quick to develop.

What is Node.js ?

Image 5

  • Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications.
  • It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • It's perfect for data-intensive real-time applications that run across distributed devices.

What are the advantages of using MEAN stack ?

  • A single language is used throughout the application.Which is JavaScript.
  • All the parts of the application can support and often enforce the use of the MVC architecture.
  • Serialization and deserialization of data structures is no longer needed hence data marshalling is done using JSON objects.

How to Install MongoDB on Windows ?

Step 1

  • My laptop is having Win 8.1 OS.So I'm going to download the Windows 64 bit Msi version.
  • You can download it from Here.

Image 6

Step 2

  • Just double-click the Msi file.
  • Then you'll see a set of screens which will appear to guide you through the installation process.
  • I accepted default settings for all the screens.

Image 7

Step 3

  • The default path for the installation as shown below.

Image 8

Step 4

  • I have created a new folder named as 'mongodb' on 'c:\' drive.
  • This step is optional.You can run MongoDB from any folder as you wish.
  • After that contents have been moved into that folder as shown below.

Image 9

How to Run MongoDB ?

Step 1

  • MongoDB requires a 'data' directory to store all the data.
  • MongoDB’s default data directory path is 'c:\data\db'.
  • I have created a 'data\db' folder on 'C' drive.It looks like below.

Image 10

Step 2

  • Run 'Command Prompt' with “Administrative Privileges”.
  • To start MongoDB, run 'mongod.exe' on command prompt as shown below.
          C:\WINDOWS\system32>cd C:\mongodb\bin
          C:\mongodb\bin>mongod.exe

Image 11

  • You can see that the main MongoDB service where it starts listening to the default port 27017.
  • Which means it starts the main MongoDB database process.
  • The 'waiting for connections' message in the console output indicates that the mongod.exe process is running successfully.

How to Create a Windows Service for MongoDB ?

  • We can use Windows Service for running MongoDB automatically after every reboot cycle.
  • Which is the more popular approach for running MongoDB on windows machines.
  • Before we begin setting up MongoDB as a Windows Service, it's considered good practice to specify a path for the MongoDB log and configuration files.Let's do it first.

Step 1

  • Create a directory inside the 'data' folder as 'log'.
  • This is for the log files.It's shown as below.

Image 12

Step 2

  • Let's create a configuration file.
  • This configuration file can include any of the configuration options for mongod.
  • But it must include a valid setting for the 'logpath' which we created on Step 1.
  • The following command creates a configuration file, specifying both the 'logpath' and the 'dbpath'settings in the configuration file.
         C:\>echo logpath=c:\data\log\mongod.log> "C:\mongodb\mongod.cfg"

         C:\>echo dbpath=c:\data\db>> "C:\mongodb\mongod.cfg"

Image 13

  • It creates 'mongod.cfg' file inside the 'C:\mongodb' folder.
  • It looks like below.

Image 14

Step 3

  • Let's create a MongoDB service.
  • For that,you have to run the below mentioned command on command prompt (which is having administrator privileges ( Run as administrator)).
C:\> sc.exe create MongoDB binPath=  "\"C:\mongodb\bin\mongod.exe\" --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"

Note : sc.exe requires a space between “=” and the configuration values (eg “binPath= ”), and a “” to escape double quotes.

  • If the service was successfully created, you will get the following log message.
 [SC] CreateService SUCCESS

Image 15

Step 4 : Run

  • We have installed our MongoDB service.Now you can run it by executing the following command in the command prompt window.
C:\>  net start MongoDB

Image 16

Step 5 : Stop

  • If you need to stop the MongoDB service, use the following command.
  C:\>  net stop MongoDB

Image 17

How to use the MongoDB shell ?

  • You can find out the MongoDB shell inside the 'C:\mongodb\bin' folder.
  • Which allows you to interact with MongoDB server instance using the command line.

Step 1

  • To start the shell, you have to navigate to the mongodb bin folder as shown below.
         C:\> cd C:\mongodb\bin

Step 2

  • After that you can run the 'mongo' service as shown below.
  C:\mongodb\bin> mongo
  • If you successfully installed MongoDB, the shell will automatically connect to your local instance, using the 'test' database.
  • You will see a console output similar to the following screenshot.

Image 18

Step 3

  • Let's do some MongoDB data manipulations.
  • Type following command on the MongoDB shell.
  > db.books.insert({title: "Hello MEAN World"})
  • The preceding command will create a new 'books' collection and insert a JSON object containing a'title' property.
  • To retrieve the book object, execute the following command.
> db.books.find()
  • The shell console will output the following message.
{ "_id" : ObjectId("54cc8cfedc0606c101f00e95"), "title" : "Hello MEAN World" }

Image 19

  • It means your MongoDB instance is working properly and you have successfully managed to interact with it using the MongoDB shell.
  • Congratulations !. You have done that.

Step 4

  • If you need to exit from the shell,just type 'exit'.
         > exit

What is Robomongo?

  • Robomongo is a shell-centric cross-platform open source MongoDB management tool (i.e. Admin GUI).
  • Robomongo embeds the same JavaScript engine that powers MongoDB's mongo shell.
  • Everything you can write in mongo shell where you can write in Robomongo.

How to Install Robomongo on Windows ?

Image 20

  • Just double click the downloaded installer set-up and accept the default settings for all the screens.That's it.

Image 21

How to work with the Robomongo ?

Step 1

  • Just double click the auto created 'Robomongo' short cut on the desk top.
  • It'll open the GUI of the Robomongo.

Step 2

  • After that you have to create a connection string for the MongoDB database.
  • You can use 'Create' link to create a new database connection as shown below.

Image 22

  • I have given a connection name as 'MongoDbTest' and Address with port number (i.e. port of MongoDB server) as 'localhost : 27017'.
  • You can test your connection by using 'Test' button as shown above.
  • After that you can use the 'Save' button to save the changes.

Step 3

  • You can do huge set of MongoDB operations by using this awesome Robomongo GUI.
  • Just a few of them are shown below.

Image 23

  • Here I have used the 'test' database with the 'books' collection where we created earlier.
  • If you need to see the full list of features, please visit their home page here.

References

  • MEAN Home

Conclusion

  • In this article, you learned basics of MEAN stack development.
  • You also learned how to install MongoDB and how to connect to your local database instance using the MongoDB shell.
  • You also learned how to install and use the MongoDB management tool Robomongo GUI.
  • In the next article,we'll discuss how to install Node.js and the basics of Node.js.
  • I hope this helps to You.Comments and feedback greatly appreciated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Sri Lanka Sri Lanka
Sampath Lokuge holds a Bachelor of Science degree in the Mathematics and Computer Science at the University of Colombo, Sri Lanka.

He possesses over 8 years of experience in constructing web applications using Microsoft Technologies including ASP.net MVC, C#, SQL Server, Web API, Entity Framework and also other web technologies such as HTML5, CSS3,jQuery and AngularJS.

Sampath has earned Microsoft certifications such as MCP, MCAD, MCSD and MCTS and very recently he has completed MS (Microsoft Specialist) for MVC 4 and MCSD (Windows Store Apps Using HTML5).

Besides that, he is an active blogger, writing about web and mobile development issues and promoting best practices.He also actively participates in online communities such as Code Project and StackOverflow.He himself is handling three communities, which are ASP.net MVC 5 With C# on Linkedin,Entity Framework 6 on G+ and Hybrid Mobile App with WinJS on Facebook.

Now, I am a 100% Freelancer. Smile | :)

Tech Blogs


Sampath Lokuge Tech Universe

Communities which I'm Handling :


Entity Framework 6

ASP.net MVC 5 With C#

Hybrid Mobile App with WinJS

Comments and Discussions

 
GeneralMy vote of 5 Pin
Debabrata_Das21-Mar-15 4:59
professionalDebabrata_Das21-Mar-15 4:59 
GeneralRe: My vote of 5 Pin
Sampath Lokuge21-Mar-15 19:31
Sampath Lokuge21-Mar-15 19:31 
GeneralRe: My vote of 5 Pin
Sampath Lokuge22-Mar-15 1:35
Sampath Lokuge22-Mar-15 1:35 
SuggestionSelf Plagiarism? or Wrong Section? Pin
Ravimal Bandara2-Feb-15 20:52
Ravimal Bandara2-Feb-15 20:52 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
Sampath Lokuge2-Feb-15 21:08
Sampath Lokuge2-Feb-15 21:08 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
Richard MacCutchan3-Feb-15 1:19
mveRichard MacCutchan3-Feb-15 1:19 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
Sampath Lokuge3-Feb-15 1:51
Sampath Lokuge3-Feb-15 1:51 
QuestionRe: Self Plagiarism? or Wrong Section? Pin
CHill603-Feb-15 2:07
mveCHill603-Feb-15 2:07 
AnswerRe: Self Plagiarism? or Wrong Section? Pin
Sampath Lokuge3-Feb-15 2:42
Sampath Lokuge3-Feb-15 2:42 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
CHill603-Feb-15 2:47
mveCHill603-Feb-15 2:47 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
Sampath Lokuge3-Feb-15 3:09
Sampath Lokuge3-Feb-15 3:09 
GeneralRe: Self Plagiarism? or Wrong Section? Pin
CHill603-Feb-15 3:15
mveCHill603-Feb-15 3:15 
GeneralMy vote of 2 Pin
CHill602-Feb-15 3:57
mveCHill602-Feb-15 3:57 
GeneralMy vote of 2 Pin
Pete O'Hanlon2-Feb-15 0:27
mvePete O'Hanlon2-Feb-15 0:27 
GeneralRe: My vote of 2 Pin
Sampath Lokuge2-Feb-15 0:36
Sampath Lokuge2-Feb-15 0:36 
Rome wasn't built in a day no.So this is the first part of the series of articles where I hope will write about the MEAN.This is just an introduction of M (MongoDB).Please wait for the others.... Smile | :)

My Latest Article : MEAN Stack Development with MongoDB

Nothing is Impossible for Willing Heart.

GeneralRe: My vote of 2 Pin
Pete O'Hanlon2-Feb-15 0:41
mvePete O'Hanlon2-Feb-15 0:41 
GeneralRe: My vote of 2 Pin
Sampath Lokuge2-Feb-15 0:45
Sampath Lokuge2-Feb-15 0:45 
GeneralRe: My vote of 2 Pin
Pete O'Hanlon2-Feb-15 1:37
mvePete O'Hanlon2-Feb-15 1:37 
GeneralRe: My vote of 2 Pin
Sampath Lokuge2-Feb-15 1:42
Sampath Lokuge2-Feb-15 1:42 
General+5 - Good One : Thank you Pin
Liju Sankar1-Feb-15 23:50
professionalLiju Sankar1-Feb-15 23:50 
GeneralRe: +5 - Good One : Thank you Pin
Sampath Lokuge1-Feb-15 23:58
Sampath Lokuge1-Feb-15 23:58 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun1-Feb-15 23:27
Humayun Kabir Mamun1-Feb-15 23:27 
GeneralRe: My vote of 5 Pin
Sampath Lokuge1-Feb-15 23:39
Sampath Lokuge1-Feb-15 23:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.