Wednesday 18 February 2015

Mongo Shell

Mongo is interactive JavaScript shell for mongoDB. When you run mongo, you will get messages like below.

MongoDB shell version: 2.6.6 
connecting to: test 


First line specifies the version of mongo shell. Second line specifies the database it connects to. It is connected to database test. “test” is the default database in mongoDB. I said, mongo is an interactive javascript shell, So you can execute javascript statements.

> for(i=0; i<10; i++) print(i);
0
1
2
3
4
5
6
7
8
9
>

How to know the database you are using 
Simply type command “db” in the shell, it will return the database you are using currently.
> db
test
>

To list all databases 
Command “show dbs” is used to display all databases.
> show dbs
admin   (empty)
local   0.078GB
m101    0.078GB
sample  0.078GB
>

To switch database
Command “use databaseName” is used to switch database.
> use sample;
switched to db sample
>

To get help 
Use the command “help” to get help.
> 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 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
>

To get all mongo shell short cuts 
Use the command “help keys”.
> help keys
Tab completion and command history is available at the command prompt.

Some emacs keystrokes are available too:
Ctrl-A start of line
Ctrl-E end of line
Ctrl-K del to end of line

Multi-line commands 
You can enter a multi line javascript expression.  If braces are not closed, you will see a new line beginning with '...' characters.  Type the rest of your expression.  Press Ctrl-C to abort the data entry if you get stuck.

To quit from mongo shell 
Press “<Ctrl–c>” or type the command “quit()”.

 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment