How to Install MongoDB with a NodeJS project and use the MongoDB Shell (windows).
In this blog post I will explain how I installed MongoDB, setup a database and then used it in my Node JS project. Let’s jump straight in!
Install MongoDB and set Location
First thing to do is download MongoDB from the MongoDB site. Once downloaded run the install and it should install to your program files. Take a copy of everything in the mongo folder and paste it into a location where you can get to it easily enough. I keep it in a folder called mongo in the C drive like this:
‘c:/mongo’
so it is quick to access from command prompt (cmd).
Install MongoDB NPM Module in NodeJS
To install MongoDB into your node project and add it as a dependency in your package.json run this in cmd at your project location:
npm install mongodb –save
Create Database for your Node JS Project
So I had a NodeJS project and I was able to serve up web pages using express etc etc. Then I wanted to be able to pass data from the client side to my Node server and save it to a database. To do this we must create the database! Open up cmd and navigate to your mongo folder we just created in the C drive. once in this location in cmd, we need to navigate to the bin folder inside our mongo folder. (This is where all the program files for MongoDb live and we need to use them from here).
Ensure you have a location for your Node JS app and create a folder in that location called ‘data’. Then from cmd we type:
mongod –dbpath C:/NodeApplications/MyNodeApp/data
and press enter. If you now go to your Node JS app folder you will see the data folder has some content inside it.
Your cmd window has initiated the MongoDB and is listening for any connections to it. Leave that cmd window and open a new one. Navigate to C:/mongo/bin like before and run:
mongo localhost:27017
and hit enterThis opens a connection using the MongoDB Shell and connects to our database we opened in the other cmd window. The reason we use localhost:27017 is because that is where the MongoDB defaults to. The cmd window will now be connected to the test database which is the default for MongoDB. To create a new database type:
use MyDB
This will create a database called ‘MyDB and open it for us in cmd. From here we can add information, remove information etc.
Add something to the database
Once we have connected to our MongoDB and opened our database, we can insert rows like this:
db.collection.insert({“FirstName” : “Harry”, “LastName” : “Jacks”
Remove something from the database
Similar to inserting a piece of data, we use:
db.connection.remove({“LastName” : “Jacks”})
This will remove all rows which LastName=Jacks.
Setting up a Collection in a Database
Once the database is open and accessed through MongoDB Shell, we can add a collection and do things with it. For example, the following example has connected to the localhost:27017..
I run:
use users
This creates a users database.
Then:
coll=db.users
This creates a users collection in my users database.
Then:
db.users.insert({“FirstName” : “Michael”, “LastName” : “Jackson”})
This adds this row of data to my users collection.
Then:
coll.find()
This will find all documents from within my users collection. Essentially this just means the items in my users collection in my users database.