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.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: