Getting Started

MongoDB client and server

MongoDB follows the classical client-server model.

In the MongoDB toolset, there are two binaries. mongo and mongod. They perform the roles of the client and server applications.

mongod runs as a service and listens for any incoming requests. It responds with results, errors.

Lets follow the basic communication model.

First, run the client, mongo by itself.

sameeri@smarryboyina MINGW32 /
$ mongo
2016-02-07T09:04:32.166-0600 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.1
connecting to: test
2016-02-07T09:04:33.231-0600 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-02-07T09:04:33.236-0600 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6

exception: connect failed

The error has a lot to say. For the client, to work, the server needs to run.

Let's go ahead and run the server, mongod then.

$ mongod
2016-02-07T09:06:08.687-0600 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] MongoDB starting : pid=10340 port=27017 dbpath=C:\data\db\ 64-bit host=smarryboyina
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] db version v3.2.1
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1p-fips 9 Jul 2015
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] allocator: tcmalloc
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] modules: none
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] build environment:
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten]     distarch: x86_64
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten]     target_arch: x86_64
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] options: {}
2016-02-07T09:06:08.697-0600 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating
2016-02-07T09:06:08.697-0600 I CONTROL  [initandlisten] dbexit:  rc: 100

Another error. When we closely look at the error description Data directory C:\data\db\ not found, we can find that the server application expects a data directory at the specified place.

MongoDB Data Directory

MongoDB as a database management system, stores data in files. Like any other DBMS software. It has a defualt data directory expectation. C:\data\db\.

If such a directory is found, it will start successfully.

So, lets create this directory structure.

sameeri@smarryboyina MINGW32 /c
$ mkdir data

sameeri@smarryboyina MINGW32 /c
$ cd data && mkdir db

and attempt a restart on the server.

$ mongod
2016-02-07T09:10:36.615-0600 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-02-07T09:10:36.615-0600 I CONTROL  [initandlisten] MongoDB starting : pid=12592 port=27017 dbpath=C:\data\db\ 64-bit host=smarryboyina
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] db version v3.2.1
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1p-fips 9 Jul 2015
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] allocator: tcmalloc
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] modules: none
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] build environment:
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten]     distarch: x86_64
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten]     target_arch: x86_64
2016-02-07T09:10:36.625-0600 I CONTROL  [initandlisten] options: {}
2016-02-07T09:10:36.674-0600 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2016-02-07T09:10:38.095-0600 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-02-07T09:10:38.095-0600 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2016-02-07T09:10:38.551-0600 I NETWORK  [initandlisten] waiting for connections on port 27017

Lo, behold. Server is ready to accept any requests and respond.

Now, the client can be started as well.

sameeri@smarryboyina MINGW32 /c
$ mongo
2016-02-07T09:11:54.172-0600 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

The client has also successfully started. The client provides a prompt. This is where any commands can be issued. The commands are sent as requests to the server.

Client-Server Communication

Let's observe the communication between the client and the server.

Our first indicators are the messages at the console.

2016-02-07T09:10:38.551-0600 I NETWORK  [initandlisten] waiting for connections on port 27017
2016-02-07T09:11:54.244-0600 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:57852 #1 (1 connection now open)

Both the messages are from the server console and they indicate that the server is ready to accept any connections. One or more clients can connect to the server. Each connection is logged.

By default the server runs on a known port, port 27012.

The client on the other hand does not display any connection info. It attempts to connect to a server and only displays connection failures. Once connected the client presents an interaction environment much like a shell. mongo client is also referred to as mongo shell and is addressed so in the docs.

Inside the shell, one can issue commands.

The shell is a JavaScript environment. The shell is a wrapper over the chrome's V8 engine. Any valid JavaScript works here.

$ mongo
2016-02-07T09:11:54.172-0600 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.1
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> 2+3
5
> "sameeri".length
7

That is all cool. But to communicate with the server, the shell provides specialized commands.

To test things out, we can execute a basic insert command and observe the server logs.

> show databases   
local  0.000GB     
> show collections 
> db               
test            
> db.myCollection.insert( { x: 1 } );
WriteResult({ "nInserted" : 1 })

In the server console,

2016-02-07T09:11:54.244-0600 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:57852 #1 (1 connection now open)
2016-02-07T09:33:21.962-0600 I COMMAND  [conn1] command test.myCollection command: insert { insert: "myCollection", documents: [ { _id: ObjectId('56b763c17123486ba4772ae8'), x: 1.0 } ], ordered: true } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } } } protocol:op_command 215ms

the insert command yields a log.

It sees the insert as a command to do work, goes ahead and executes the command and sends a message back to the client saying that the record was successfully inserted.

If yet another command is issued, this time the find:

> db.myCollection.find()
{ "_id" : ObjectId("56b763c17123486ba4772ae8"), "x" : 1 }

Nothing is written at the server console.

Information, data fetches are not reported at the server console. But the communication still happens. Although in the background. The messages can be observed by increasing the verbosity levels for the server.