How to install mongodb and connect to your project part-1

Folks,

Lets continue with my previous post where i created a project named nodetest in d drive. For instance, lets use the same project here to install and use mongodb.

Before stepinto that..lets know briefly what is mongodb

MONGODB

  • NoSQL database
  • Well documented, huge community
  • Databases = Databases
  • Collections = Tables
  • Documents = Rows
  • Nested documents

For more on documentation of mongodb clickhere

There are many libraries like mongodb,mongoose etc in market. To know the major difference between mongoose and mongodb? clickhere

First download exe file from mongodb.org

Now install it in your machine. Here my path of mongo db installation is C:\Program Files\MongoDB 2.6 Standard

Lets move to next step

RUN MONGOD AND MONGO

In your nodetest directory, create a subdir called “data”. Then navigate to the directory in which you placed your MongoDB files (let’s say C:\Program Files\MongoDB 2.6 Standard\bin). From that directory, type the following:

COMMAND C:\MONGO\
mongod --dbpath c:\node\nodetest1\data

28-05-2014 16-22-23

You’ll see the Mongo server start up. This is going to take a while if it’s the first time, because it has to do some preallocating of space and a few other housekeeping tasks. Once it says “[initandlisten] waiting for connections on port 27017”, you’re good. There’s nothing more to do here; the server is running. Now you need to open a second command prompt. Navigate again to your Mongo installation directory, and type:

COMMAND C:\Program Files\MongoDB 2.6 Standard\bin
mongo

You’ll see something like the following:

MONGO CONSOLE
c:\mongo>mongo
MongoDB shell version:2.4.5
connecting to: test

28-05-2014 16-23-53

 

Don’t worry about “connecting to: test” … that’s just the default database Mongo decides to use if you don’t specify one on the command line, which we didn’t because it’s not important right now. It doesn’t actually even create the “test” database unless you add a record. It’d be totally fine to just work in that database for right now, but let’s make one of our own. In your Mongo console, type the following:

MONGO CONSOLE
use nodetest

28-05-2014 16-44-14
From the above shown image you can see, Switched to db nodetest after you hit enter.

To make the database exist, we have to add some data. We’re going to start off by doing that right inside of the Mongo client.

 ADD SOME DATA

Let’s add a record to our collection. For the purposes of this tutorial, we’re just going to have a simple database of usernames and email addresses. 

In your Mongo client, type this:

MONGO CONSOLE
db.usercollection.insert({"username":"sathya","email":"sathya@log.com"})

28-05-2014 16-49-35

You can see inserted as a result once you hit enter

Something important to note here: that “db” stands for our database, which as mentioned above we’ve defined as “nodetest1”. The “usercollection” part is our collection. Note that there wasn’t a step where we created the “usercollection” collection. That’s because the first time we add to it, it’s going to be auto-created.

Type this:

MONGO CONSOLE
db.usercollection.find().pretty()

In case you’re curious, the .pretty() method gives us linebreaks. It will return as shown below..

28-05-2014 16-53-03

let’s add a a couple more. In your Mongo console, type the following:

MONGO CONSOLE
newstuff =[{"username":"abc","email":"abc@domain.com"},{"username":"xyz","email":"xyz@domain.com"}]
db.usercollection.insert(newstuff);

Note that, yes, we can pass an array with multiple objects to our collection. Handy! Another use of db.usercollection.find().pretty() will show all three records.

Reference: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

In above process, we gave db path for the application folder where db itself maintained in project folder. Else you can try in different way like how mysql stores all projects in bin of mysql. You can achieve the same by the following video. Give a try.

Download git from here

Advertisements

One thought on “How to install mongodb and connect to your project part-1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s