Sei sulla pagina 1di 48


Ravikumer Maddi

Object Id:
ObjectId is a 12-byte BSON type, constructed using:

a 4-byte value representing the seconds since the Unix epoch,

a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.

Insert Title Text



Insert Title Text

Meta data Tables, columns,

No Meta data What you need to keep
you can keep, no need to obey any
help admin
help connect
help keys
help misc
help mr

help on db methods
help on collection methods
sharding helpers
replica set helpers
administrative help
connecting to a db help
key shortcuts
misc things to know

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 with time >= 1ms
show logs
show the accessible logger names
show log [name]
prints out the last segment of log in memory, 'global' is default
use <db_name>
set current database
list objects in collection foo { a : 1 } )
list objects in foo where a == 1
result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
quit the mongo shell

Create replica
cd c:\data
md db1
md db2
md db3
cd c:\mongodb\bin
@REM Primary
start "a" mongod --dbpath ./db2 -port 30000 --replSet "demo"
@REN Secibdart
start "b" mongod --dbpath ./db2 -port 40000 --replSet "demo"
@REN Arvuter
start "c" mongod --dbpath ./db2 -port 50000 --replSet "demo"

Then it will open three consoles for three replicas.
then to connect to perticular replica run:
mongo --path 30000

To know on which replica(db) you are using:


Initiate the Replicas

Access data from Secondary created in Primary:

You should setSlaveOk() to get the data.

. Close primary console(a) and
. Secondary becomes as
Primary because primary was
. And you can find the data and.
The port was 40000(Seconday).
Ie. Seconday become as Primay.
To recreate the primary:
. Come out of mongo,
. Run the same command that
you run last time to start.
. Then again start mongo with
which replica you want.

How to run a .js file

> mongo userAccount.js

> mongo localhost/admin --eval
> mongo localhost/admin --eval

. This is tool to create schedules.

. To run some administrative activities as background.

Prevent accidental data lose.

DB.prototype.dropDatabase = function(){
print("Don't do it man! ");
db.dropDatabase = DB.prototype.dropDatabase
mongo saveDB.js shell
Try to delete
> db.dropDatabase()

Prevent accidental data lose.

Prevent accidental data lose.

Prevent accidental data lose.

Prevent accidental data lose.

Prevent accidental data lose.

It opens in notepad++ and now you can change and

save and close.

See, It is working now.

Overall Good Points:

To know the where you are in tha mongo shell run

This is the file(.mongorc.js) to automatically recognize by the mongo and run
after mongo shell(mongo) started and no need to maintain in separate .js
files and remember it.

It should be c:\users\<yours>\.mongorc.js
See here it is.

Disable .mongorc.js run

mongo --norc
If you start shell with above command it
.mongorc.js dont run after your shell started.

You should prevent two things much in MongoDB