Zhixian's Tech Blog


MongoDB Nutshell

Filed under: web application development — Tags: , , , , , , — Zhixian @ 11:12:03 am

I decided that my previous notes on MongoDB quickstart is too wordy. Here’s a more succinct version.


  1. Get and unzip MongoDB files from http://www.mongodb.org/downloads into MongoDB directory (eg. C:\MongoDB)
  2. Create a Data directory in MongoDB directory (eg. C:\MongoDB\Data\Test)
  3. Start MongoDB server using C:\MongoDB\mongod.exe –-dbpath C:\MongoDB\Data\Test
  4. Start MongoDB client using C:\MongoDB\mongo.exe

Mongo Client Command Basics

help – display a summary of commands available
db – indicates the database that you are using
show dbs – list databases
use demo – switch to use database call demo
book1 = { author:”Lewis Caroll”, title:”Alice in Wonderland” }defines a document call book1 with attributes author and title.
db.books.insert(book1) – insert “book1” into a collection call “books”
db.books.find() – list documents (as cursor) in “books” collection
db.books.find({title:”Alice in Wonderland”}) – list documents (as cursor) with title of “Alice in Wonderland” in “books” collection

db.books.find().limit(3) – limits the numbers results in list results
db.books.findOne() – return a document in “books” collection

Other tricks – variables & loops

for (var i = 1; i <= 20; i++) db.things.insert( { x : 4 , j : i } )
var c = db.things.find()
while ( c.hasNext() ) printjson( c.next() )
printjson( c [ 4 ] )

Side Notes

A MongoDB is datastore with many databases. Each database may contain multiple collections of free-form documents.
MongoDB will create collections and databases implicitly upon their first use (insertion of data), hence no creation statements are needed.
All records (documents) in MongoDB are dynamic, hence no schema needs to be defined.

MongoDB Up and Running

Filed under: web application development — Tags: , , , , — Zhixian @ 10:25:03 am

This is my notes to quickly get started with MongoDB under Windows environment.
Most of the material here can be found from the MongoDB Manual.

This post covers the following:

  1. Materials required
  2. Installation
  3. Running MongoDB  Server
  4. Running MongoDB Client

Materials required – 1 required

  1. MongoDB (required)
    Download from http://www.mongodb.org/downloads
    As of date (2013-03-02), the production version is 2.2.3.
    I’m using the Windows 64-bit (2008+) build since this as said to enhance performance when running MongoDB with 64-bit Windows.


Installation of MongoDB is straight-forward as it is simply a zip file.
Simply extract the contents of the zip file to a convenient location like C:\MongoDB.
The contents of the zip file should look like the below:


All the executables related to MongoDB are found in the bin folder.

Before proceeding further, you should create folder(s) to hold your data as well.

I try to keep to this convention on naming directories to store MongoDB data:<MongoDB directory>\Data\<application>


  • <MongoDB directory> – refers to the directory where I extracted the MongoDB files.
  • <application> – refers to the name of the application that MongoDB is used to store data for.

For this post, I will store my data in C:\MongoDB\Data\Quickstart

Running MongoDB Server

MongoDB server is run by executing mongod.exe executable file. Basic execution of this file takes the following syntax:

mongod.exe –-dbpath <data directory path>

<data directory path> refers to the directory that you will store MongoDB data.
As per installation step, we will be using C:\mongodb\data\quickstart for example.

To run MongoDB server from the command-line, go to MongoDB’s bin directory and execute the following command:

mongod.exe –dbpath C:\mongodb\data\quickstart


If the directory that you specify does not exists, you may get the following screen:


To resolve the error, simply create the directory before running the command again.

The first time, you run mongod.exe you may get a firewall warning like the below:


For security, ensure that only the Private networks, such as my home or work network option is checked before clicking on the Allow access button. This should be fine for general local development and testing purposes.

If MongoDB has started, you should see the following screen:


The important thing to note, is the last line.
It tells you that MongoDB has started and is waiting for requests on port 28017.

Running MongoDB Client

MongoDB client is run by executing mongo.exe executable file, in the MongoDB directory.

The first time you run this executable, you will get a introductory text as shown in the the following screen.


Subsequent executions, will not display the introductory text.


Create a free website or blog at WordPress.com.