Skip to main content

How to setup MongoDB on Windows

To start working with MongoDB, we need to first understand the mongodb structure, where it maintains the data and importantly the logs.

Installation

Setting mongodb on Windows based PC is quite simple. Just install the desired version from MongoDB Website. For development and learning, we should use Community edition.

Just complete the installation process, simply following the instructions. You may leave everything as its default. I generally prefer installing it out of Program files, like D:\ etc.

Directory Setup

Let's assume you installed it in M:\mongodb

By default, on a Windows system, the installation process creates its data directory as C:\data. But it's all under our control, how we want to install its service.

Let's define some directories..

conf: for configuration files
data: for data storage location


Now under data, we need to create db and log directories.

db: where the actual database is stored
log: mongodb log file location

Service Setup

At this point, we are done defining the directories. Now, we need to tell MongoDB service about these custom directories. There are two ways to do that.
Either we can directly add this parameter while creating the MongoDb service or we can rather create a configuration file and use the same while creating Service.

Define Configuration file

As we've kept M:\mongodb\conf for configuration files, let's create a file mongod.conf under the directory. A typical structure would contain following things:
systemLog:
    destination: file
    path: M:\mongodb\data\log\mongod.log
storage:
    dbPath: M:\mongodb\data\db
net:
    bindIp: 127.0.0.1
security:
    authorization: disabled

net.bindIp: This config doesn't actually matter on a developer machine it should be set to localhost (127.0.0.1). On server machines, if we want to access the DB from any other machines, then we can either make it public (0.0.0.0) or some specific VLANs.

security.authorization: Either enabled or disabled. Let's keep it disabled for now. We'll see later how to create admin user and enable authorization. Learn more about it at How to Create User in MongoDB

This configuration file must not have any Tab space.


Now, let's create the MongoDB service using our configuration file
mongod --config "M:\mongodb\conf\mongod.cfg" --serviceName "MongoDB" --serviceDisplayName "MongoDB" --serviceDescription "MongoDB Server Instance" --install

For removing the service:
mongod --remove

Start MongoDB Service

If the above command runs successfully, you should be able see a new service under Windows Services. Start it either from console or command prompt (admin mode)

START: net start MongoDB
STOP: net stop MongoDB

Service name on command prompt is case-insensitive.


Connect to MongoDB

Add mongo to your System Path. Run below commands on a command window, opened as Admin
#Add MONGO_HOME variable
SETX MONGO_HOME "M:\mongodb" /M

#Add MONGO_HOME variable to System Path
SETX PATH "%%MONGO_HOME%%\bin\;%PATH%;" /M
Open new cmd window and use mongo command to connect to DB.


Comments

Popular posts from this blog

MongoDB BulkWrite Java API

Since version 3.2, MongoDB has introduced Bulk Update methods. In context of RDBMS, it's like SQL Batch Jobs, where SQL Statements are prepared in different chunks and a batch of statements are submitted to DB for update/insert. Here are some important points about MongoDB Bulk Write operation.. Useful in case you've huge data to update/insert. Mongo automatically prepares batches (of 1000 default) and start execution in an ordered/unordered manner. This drastically reduce DB trip time. Let's say there are 50 thousand records to update, now instead of 50k round trips to DB from your app server, using Bulk Update it would be reduced to just 50 round trips. Let's see an example below: List<WriteModel<Document>> updateDocuments = new ArrayList<WriteModel<Document>>(); for ( Long entityId : entityIDs ) { //Finder doc Document filterDocument = new Document (); filterDocument . append ( "_id" , ent

MongoDB fetch operation using Java API

With changes in MongoDB, there has been several changes in its Java API as well. There are now some good and easy way to perform different operation with DB. MongoDB Java API is really very simple and easy to understand. If we understand the basics, we can build up simple to complex queries. Let's take a look at the example of MongoDB fetch operation using the new Java API and then we'll try to understand the basics.. public MyEntity findMyEntityById ( long entityId ) { List < Bson > queryFilters = new ArrayList <>(); queryFilters . add ( Filters . eq ( "_id" , entityId )); Bson searchFilter = Filters . and ( queryFilters ); //Fields to return. //_id is available by default. A value of "0" would skip it List < Bson > returnFilters = new ArrayList <>(); returnFilters . add ( Filters . eq ( "name" , 1 )); returnFilters . add ( Filters . eq ( "_id" , 0 ))

MongoDB Aggregation using Java API

A very common problem scenario in programming is to get the records or record count by certain fields. For developers familiar with RDBMS, it's like creating a SQL with combination of count function and group by attributes. For MongoDB too, it's very similar. Let's look at the example below fetching no of employees group by department Ids. public Map < Long , Integer > getEmployeeCountMapByDeptId () { Map < Long , Integer > empCountMap = new HashMap <>(); AggregateIterable < Document > iterable = getMongoCollection (). aggregate ( Arrays . asList ( new Document ( "$match" , new Document ( "active" , Boolean . TRUE ) . append ( "region" , "India" )), new Document ( "$group" , new Document ( "_id" , "$" + "deptId" ). append ( "count"