Sunday 27 March 2016

Install Mongodb on Windows


Please follow below steps to install mongodb on windows :

1) Download latest version of mongodb from https://www.mongodb.org/downloads#production


2) Run the .msi file , Accept the agreement & install the mongodb on your machine.

3) After successful completion of installation you will find "MongoDB" folder under "C:\Program Files"

4) Go to "C:\Program Files\MongoDB\Server\3.0\bin" path. Here you will find multiple application files. We are going to see use of each application as we move further :


  • mongod : Its a server component. mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations. It supports different options like :
    • --help : returns infomation about options and use of mongod
    • --version : returns mongod release number
    • --config <filename> , -f <filename> : specifies configuration file for runtime configuration option.
    • --quiet : runs mongnd in quite mode that limits the no of output.

  • mongo : Its a client component. mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. It supports different options like 
    • --npdb : prevents shell to connect default connection with db. Post connection you can connect to database by creating object as conn = new Mongo(); db = conn.getDB("myDatabase");
    • --host <hostname> : used to connect to specific host.
    • --version : returns mongo release number.
    • <db address > : for ex : mongo 127.0.0.1/test
    • <js file> : specify the javascript file to run and exit.
5) Add path to environment variable.  Right click on "My Computer" -> Properties -> "Advanced System settings" -> "Environment Variable" -> select "Path" in system variables -> Edit -> Append this (;C:\Program Files\MongoDB\Server\3.0\bin;) path to variable value -> Apply / Ok the changes.









6) Run the command propt. To start the mongodb server , Run "mongod" : This will open the database so that we can connect to it.


This this window open. It will keep listening for database connection on particular port (27017 in this case). Log will also get append in this screen as client runs the command.


7) To connect to server as client, Open another window of command propt -> run "mongo". It will get connected to default test database.


Use "db" command to check you have successfully connected.

You can also see additional logs on server (mongod) window :



To exit from client connection , use either "exit" command or "ctrl + c" 
To shutdown the mongod connection use "ctrl + c". It will also kill all the client connection.

Basic Mongodb commands ... To be continued...




Basic commands in mongodb


Lets continue with Basic commands in mongodb :


  • db : This command returns the name of connected database.

C:\Users\Nagendra>mongo
2016-03-28T21:35:56.979+0530 I CONTROL  Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.0.10
connecting to: test
>
> db
test
>


  • show databases : This command returns the list of all databases on the server.
> show databases
local  0.078GB
pcat   0.078GB
test   0.078GB


  • show collections : This command returns the list of all collections in current database.

> show collections
names
system.indexes
> 


  • use <database_name> : Used to switch between database. It will create new database if not already present.

> db
test
> use pcat
switched to db pcat
> db
pcat
> show collections
product
products
products_bak
system.indexes
> 


  • help : returns commands information

> 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
        exit                                  quit the mongo shell
> 



What is JSON , Insert & find queries ? ...To be continued.