How to Access MongoDB Instance 4.2?

As we discuss how to install MongoDB 4.2 on Windows7 in our previous Article How to Install MongoDB on Windows System?, we will discuss in this one How to Access MongoDB via:

  • the MongoDB Shell
  • the MongoDB Driver

1. Access MongoDB via the MongoDB Shell:

MongoDB Shell is included in the MongoDB package .It is an interactive JavaScript interface to query and update data, as well as performing administrative functions.

So to use this interface, we need to provide the full path since it is located in the same place as the other binaries for example in our case “C:\Program Files\MongoDB\Server\4.2\bin” or we can add this to our PATH.

However, before we can access MongoDB, we have to start a MongoDB instance using the mongod command in windows 7 or greater:

C:\Users\MY>”C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe”

2019-12-26T13:25:10.998+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify –sslDisabledProtocols ‘none’

2019-12-26T13:25:11.620+0100 I CONTROL [initandlisten] MongoDB starting : pid=7364 port=27017 dbpath=C:\data\db\ 64-bit host=MY-PC

2019-12-26T13:25:11.620+0100 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2

2019-12-26T13:25:11.620+0100 I CONTROL [initandlisten] db version v4.2.2

2019-12-26T13:25:11.620+0100 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf

2019-12-26T13:25:11.620+0100 I CONTROL [initandlisten] allocator: tcmalloc

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] modules: none

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] build environment:

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] distmod: 2012plus

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] distarch: x86_64

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] target_arch: x86_64

2019-12-26T13:25:11.621+0100 I CONTROL [initandlisten] options: {}

For Linux or Centos 7, to start MongoDB, use mogod command as follows:

#sudo systemctl start mongod.service

Now we can connect to our MongoDB instance and start working with it. However connection to MongoDB instance differs from local instance to remote instance:

 a. Connect to local MongoDB instance :

Running local MongoDB instance on default Port:

C:\Users\MY>cd C:\Program Files\MongoDB\Server\4.2\bin

C:\Program Files\MongoDB\Server\4.2\bin>mongo

MongoDB shell version v4.2.2

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { “id” : UUID(“b8bd6158-779d-40ca-b683-3bd40075ac2c”) }

MongoDB server version: 4.2.2

Server has startup warnings:

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten]

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten]

Enable MongoDB’s free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

>

Running local MongoDB instance on non-default Port:

C:\Users\MY>cd C:\Program Files\MongoDB\Server\4.2\bin

C:\Program Files\MongoDB\Server\4.2\bin>mongo –port 27100

 

b. Connect to remote MongoDB instance:

  • Using the Connection String :

C:\Program Files\MongoDB\Server\4.2\bin>mongo “mongodb://mongodb.server.com:27111”

  • Using the command-line option –host <host> :< port>.

C:\Program Files\MongoDB\Server\4.2\bin>mongo –host mongodb.server.com:27111

  • Using the host <host> and –port <port> command-line options:

C:\Program Files\MongoDB\Server\4.2\bin>mongo –host mongodb.server.com – port 27111

After we connect to our running MongoDB instance, we can use it for example we can check our databases list using the command show dbs as follows:

C:\Program Files\MongoDB\Server\4.2\bin>mongo

MongoDB shell version v4.2.2

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { “id” : UUID(“927c02f6-b55d-4282-9c3c-dcc4725212ed”) }

MongoDB server version: 4.2.2

Server has startup warnings:

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten]

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2019-12-26T10:15:15.929+0100 I CONTROL [initandlisten]

Enable MongoDB’s free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

> show dbs

admin 0.000GB

config 0.000GB

dbHela 0.000GB

local 0.000GB

>

We can check our currently selected database using the command db as follows:

> db

test

>

We can switch to specific database using the use database_name statement as follows:

> use DbHela

switched to db DbHela

> db

DbHela

To exit the MongoDB Shell

Type quit():

> quit ()

C:\Program Files\MongoDB\Server\4.2\bin>

Or use the <Ctrl-c> shortcut:

> ^C

bye

C:\Program Files\MongoDB\Server\4.2\bin>

2. Access MongoDB via the MongoDB Driver:

To access MongoDB instance within your programming environment, we need an official client library for our applications. This official client is called the MongoDB Driver.

The MongoDB website maintains a list of MongoDB drivers that can be used to connect to MongoDB.

This page includes drivers for the following languages:

CC++ (legacy)
C#Go
JavaNode.js
PerlPHP
PythonMotor (Python async)
RubyMongoid (Ruby ODM)
ScalaSwift

 

About Hela Khazri

Oracle Professional Certified Administrator (OCP 12c ) with 8 years of experience as Senior Oracle Database Administrator and Project Manager using scrum master methodology. A proven ability to maintain the high availability, to monitor the database performance and the security of databases - Having good analytical skills and detail-oriented to solve database issues and to assist the day-to-day support (L2 & L3);

Leave a Reply

Your email address will not be published. Required fields are marked *