You are on page 1of 48

MongoDB

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


Tab-1

Tab-1

Insert Title Text


RDBMS
Meta data Tables, columns,
relationships

MongDB
No Meta data What you need to keep
you can keep, no need to obey any
structure.

db.help()
db.mycoll.help()
sh.help()
rs.help()
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
mapreduce

Continue
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
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 further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit
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"

Notedown:
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:


db.getMongo()

Initiate the Replicas

Access data from Secondary created in Primary:

You should setSlaveOk() to get the data.

Failover
. Close primary console(a) and
observer.
. Secondary becomes as
Primary because primary was
closed.
. 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

--eval
Commands:
> mongo localhost/admin --eval
"db.runcommand({logRotate:1})"
> mongo localhost/admin --eval
"printjson(db.runcommand({logR
otate:1}))"

. This is tool to create schedules.


. To run some administrative activities as background.

Prevent accidental data lose.

saveDB.js
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
Pwd()

MongoRC.js
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