Skip to content

Install and Setup MongoDB

January 23, 2014

How to Install

General installation documentation:

http://docs.mongodb.org/manual/installation/

Linux

Ubuntu/Debian

$ sudo apt-get install mongodb-server

Red Hat/CentOS/Fedora

$ sudo yum install mongo-10gen mongo-10gen-server

Windows

  1. Download from: http://www.mongodb.org/downloads
  2. Unzip and place somewhere
    • I recommend placing it at the root of the C drive:  C:\
  3. Add to environment variables
    • For help on how to do this see: https://softwaresanders.wordpress.com/2013/12/19/windows-environment-variables/

Mac

$ brew install mongodb

Set up

How To Use (from command line)

Start MongoDB

To start up MongoDB

$ mongod

To start up MongoDB with a specific db path specified

$ mongod --dbpath /path/to/file

To start up MongoDB on a specific port

$ mongod --port 12345

Stop MongoDB

In command line you started the MongoDB in, execute:

CTRL + C

Next you have to kill the background process that was started:

$ kill <mongod-process-id>

Start MongoDB Commend Line

After you have started the MongoDB, run the the following command in another window:

$ mongo

Show Databases

First run “Start MongoDB Command Line”

> show dbs
> show databases

SwitchTo/Create Database

First run “Start MongoDB Command Line”

> use <database-name>

Delete Database

First run “Start MongoDB Command Line”

> use <database-name>
> db.dropDatabase()

Show Collections

First run “Start MongoDB Command Line” and switch to a specific DB

> show collections

Drop a Collection

First run “Start MongoDB Command Line” and switch to a specific DB

> db.<collection>.drop()

Insert Document into a Collection

First run “Start MongoDB Command Line” and switch to a specific DB

> db.<collection>.save( <some-json> )

Note: if the collection you specified doesn’t exist it will be created automatically

Query a Collection

First run “Start MongoDB Command Line” and switch to a specific DB

> db.<collection>.find()

To have the response look nice:

> db.<collection>.find().pretty()

Remove Document from a Collection

First run “Start MongoDB Command Line” and switch to a specific DB
To remove all documents from a collection:

> db.<collection>.remove()

To remove all documents from a collection:

> db.<collection>.remove( <some-json> )

Example:
> db.<collection>.remove( { "_id" : ObjectId("123456789101112131415171") })

Helpful Links:

http://docs.mongodb.org/manual/

http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell/

Advertisements

From → Guide, Installation

One Comment
  1. Hi! Someone in my Myspace group shared this site with us so I
    came to give it a look. I’m definitely enjoying the information.
    I’m book-marking and will be tweeting this to my followers!

    Wonderful blog and brilliant design and style.

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

%d bloggers like this: