Skip to main content

How to setup MongoDB tools to test/develop applications in C#?


In this article, I will be mentioning about how to install MongoDB infrastructure to develop any application in C#. After you have read this article, you will be ready to develop C#-MongoDB applications by following the below instructions step by step.

Versions of tools used in this demo:

  • Visual Studio 2010
  • .NET 4.0
  • MongoDB C#/.NET driver Version 1.11.0
  • MongoDB 3.4.0
  • Robomongo 0.9.0


1- Download MongoDB

To download latest version of MongoDB, click here.
Below page is opened. According to your Operating System, select MongoDB version and click to "DOWNLOAD". After downloading, go ahead and install MongoDB.



2- Download RoboMongo

Click here to download latest version of RoboMongo.
As December 2016, latest version of RobomMongo is 0.9.0. To download the latest version, click "Download" button. If you want to download previous versions, click on "Download previous version"


After that, select the OS and download RoboMongo. Go ahead and install the downloaded software.







3- Add new  Environment variables.

Default installation directory will be like "C:\Program Files\MongoDB\Server\3.4\bin". Add this directory to the environment variables with the name of "PATH" as following:



From system properties on "Advanced" tab, click on "Environment Variables" button. Click "New.." on user Variables section and type "Variable name" and "Variable value" as "PATH" and "C:\Program Files\MongoDB\Server\3.4\bin" respectively.



4- Create MongoDB Directory Structure  on your PC

Conventionally, MongoDB directory is created under drive D. Create a folder named "MongoDB" under D (i.e. D:\MongoDB).
Create folder named "data" under D:\MongoDB (i.e. D:\MongoDB\data)
Create folder named "db" under D:\MongoDB\data (i.e. D:\MongoDB\data\db)
Create a file named "mongod.cfg" under MongoDB (i.e. D:\MongoDB\mongod.cfg)
Paste the following code into this configuration file:

dbpath = D:\MongoDB\data\db
logpath = D:\MongoDB\mongod.log
logappend = true

5- Create Windows Service for MongoDB process.


Run "Command Prompt" with Administrative privileges i.e. Either click right and click on "Run as Administrator" or press "Ctrl + Shift + Enter" on  Command Prompt App.


To create windows service, run following command on command prompt:

Be careful that you run following code when you are in the directory where "sc.exe" is located. This executable file is generally located in "C:\Windows\System32\". If you are not in this directory in command prompt, open this directory using "cd C:\Windows\System32" command. Then proceed safely.

sc.exe create MongoDB binPath= "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe 
--service --config=\"D:\MongoDB\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

After Windows service is created, you will see that a service named "MongoDB" exists in the service list and its status is running as follows:



You can use following command line codes to start and stop this service:
To start: "net start MongoDB"
To end: "net stop MongoDB"


After you stopped this service, you can observe from the services list that, current status of service MongoDB is not "Running" as shown in below figure.




6- Download MongoDB C# Drivers


The next step is the downloading C# drivers. Before proceeding, remember and be careful while selecting the compatible C# driver of MongoDB with your .NET version. The platform that C# drivers will be downloaded is nuget store. Go to nuget store to download official C# drivers


  • If you still do not have nuget store on your Visual Studio, follow these steps:


Click on "Tools -> Extension Manager"


Type "nuget" on search screen and install "Nuget Package Manager".





  • If you have already have nuget store, follow these steps:




Search "mongo" and install the first two drivers namely: "MongoDB.Driver" and "(Legacy)Official  C# driver for MongoDB". 



However, these two drivers are compatible with .NET 4.5 . If you have an earlier version of .NET (i.e. .NET 4.0), you should install earlier versions of these drivers. (i.e. Version 1.11 or Version 1.10)
To install earlier versions of drivers, you should open "Package Manager Console" first. Then execute following command: "Install-Package mongocsharpdriver -Version 1.11.0" (without quotes)


After you have installed drivers successfully, you will see that two dll files added to your reference list automatically namely, "MongoDB.Bson" and "MongoDB.Driver".





7- Connect RoboMongo

  • Open RoboMongo application.
  • Click on "create" to create a link to listen database


You can name the name of the connection as you desired. Address is by default localhost and the port number is "27017". You can modify this part as you desired, but the simplest method is to use the local host and local port number. Then. click on "Save" button.



Conclusion
Finally, you are all set with the infrastructure related to C# and MongoDB. Now you can develop any application on C# to read and write the database MongoDB.  



Comments

Popular posts from this blog

How to Fill out a Cheque?

In this post, I will demonstrate how to fill out a cheque properly. You can have a cheque book either from your bank or any store. If you find these two suggestions expensive, you could also design and print out your own cheque. In Figure 1, a sample cheque is displayed and in Figure 2, some regions of the sample cheque are highlighted. Figure 1: Sample unfilled cheque. Figure 2: Some regions are highlighted in sample cheque. 1- Name and Address of Payer: Name and  address of the person who is the owner of this cheque and is filling out the cheque should be written in this region. 2- Date: For any official document, date is a mandatory field and hence you should write current date in this region. 3- Payee Name: Payee could be either a person or an organization (Store, Restaurant, Company, School etc.) You should write the name of the payee here explicitly. 4- Amount of Payment (Numeric): Exact amount of the payment should ...

ESP8266 Mini Project: GPIO with Button and LEDs

In this post, I'll be sharing a mini project of mine, again using ESP 8266 NodeMCU chip and MicroPython. This project mainly focuses on GPIO pins and methods for this chip. LEDs will be my output and I'll use a button as an Input. My input button will be used as a Start/Stop command for ongoing operation. Ongoing operation will be blinking of LEDs with varying frequency. I'll implement this project in two different methods: Using Asynchronous Polling Using Interrupt Request Setting Up Circuitry GPIO 16, GPIO 5, GPIO 4, GPIO 0, GPIO 2, GPIO 14 pins will be configured as Output pins being pin_D0, pin_D1, pin_D2, pin_D3, pin_D4, pin_D5 respectively. GPIO 12 will be configured as Input pin and will be connected to a small button circuitry. Button simply behaves as a switch and default position is OPEN (no contact between side pins). By default, logic LOW (0) is applied to input pin, when button is pressed (switch is closed) logic HIGH...

Mobile App Review: Amnesty SOS

A. INTRODUCTION In this post, I will review Amnesty International's mobile app for you on both Android and iOS platforms. Amnesty International has already been doing its own part for years successfully to: Stop violation of human rights  Protect people and communities who are in immediate danger Now it is your turn to do your part and  Join this great community by taking actions for human rights Speak out for people facing with unlawful, inhumane treatment Encourage friends and family to raise their voices for human rights Collect more signatures and amplify your impact  Share on social media Figure : Amnesty SOS App on Google Play Store Figure : Amnesty SOS App on iOS App Store Android App details are as follows: Version : 1.1 Released on : Feb 20, 2018 Developer e-mail : digitalcomms@amnesty.ca iOS App details are as follows: Version : 1.1.1 Released on : April 2017 Developer e-mail : digitalcomms@amnesty.ca B. ...