MongoDB Hello World Example

In this tutorial we will take first step to write Hello World Example in the MongoDB. Here
a quick guide to show you how to do basic operations like create, update, find, delete record and indexing in MongoDB. This example is using MongoDB 2.2.2, running on Window 7 OS 64 bit, both MongoDB client and server console are run on localhost, same machine.

1. Install MongoDB-
Just after downloading the MongoDB zip from its official site and unzip these file and locate to the c:/mongodb/ folder. Open command prompt and change directory to the c:/mongodb/bin/
and run the following command

c:/mongodb/bin>mongod

then you will get the following screen

2. Connect MongoDB-
To connect MongoDB, uses

c:/mongodb/bin>mongo

3. Create a database or table-
In MongoDB, both database and table are created automatically when the first time data is inserted. Uses use database-name, to switch to your database (even this is not created yet).

In below example, after you inserted a single record, database “dineshonjavaDB”, and table “employees” are created on the fly.

10. Help-
At last, uses help() to guide you how to do things in MongoDB.help – All available commands.

> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries wit
h time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memor
y, 'global' is default
use set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to f
urther iterate
DBQuery.shellBatchSize = x set default number of items to display on s
hell
exit quit the mongo shell
>