Insert Operation In MongoDB Using Robo3T

In this article, we will learn how to insert data in MongoDB using Robo 3T. We will learn from basics because I have written this article focusing on the beginners. We will also learn how we can setup MongoDB environment, with an example using console application.

MongoDB is one of the most popular NoSQL databases. Learn more here.

MongoDB Environment
Step 1
Download the MongoDB Installer from this link. Now, run this installer to install MongoDB.

Insert Operation in MonGoDB 

Click on the "Next" button.

Step 2

Now, download Robo 3T and install that from the following link.

Insert Operation in MonGoDB
Extract and install.
Insert Operation in MonGoDB
Robo 3T is a cross-platform MongoDB Management UI Tool. You can learn more about it from here.
After installation, connect to the local server.
Insert Operation in MonGoDB 
Add the name, while the default address is localhost:27017. Test the connection and click on the "Save" button.
Insert Operation in MonGoDB 
Click on "Connect" button.
Insert Operation in MonGoDB 

Now, MongoDB is connected to our system. Open Visual Studio and create a new console application. Rename it as InsertOperation.

Insert Operation in MonGoDB 

Now, add MongoDB Driver for C# using NuGet Package Manager.

Insert Operation in MonGoDB 

Step 1

Now, open program.cs and add a connection string and add the string into client object.

  1. var  Mongodbconnection = "mongodb://localhost";
  2. var Client = new MongoClient(Mongodbconnection);

Step 2

Create a database with a name Employee using Robo 3T.

Insert Operation in MonGoDB 

Step 3

Create a collection (table) with a name EmployeeDetails.

  1. var collection = DB.GetCollection<BsonDocument>("EmployeeDetails ");   

Step 4

Now, add some data in the table.

  1. var collection = DB.GetCollection<BsonDocument>("EmployeeDetails");  
  2.            var Emp = new BsonDocument  
  3.            {  
  4.                {"Name","Sanwar"},  
  5.                {"City","Jaipur"},  
  6.                {"Age","23"},  
  7.                {"Department","Software Development"},  
  8.                {"Technology","Dot Net"}  
  9.            };  

Step 5

Now, add the Emp array into collection, use InsertManyAsync function is to add the array into the collection.

  1. collection.InsertOneAsync(Emp);  

Check the complete code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using MongoDB.Driver;  
  7. using MongoDB.Bson;  
  10. namespace InsertOperation  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             var  Mongodbconnection = "mongodb://localhost";  
  17.             var Client = new MongoClient(Mongodbconnection);  
  18.             var DB = Client.GetDatabase("Employee");  
  19.             var collection = DB.GetCollection<BsonDocument>("EmployeeDetails ");  
  20.             var Emp = new BsonDocument  
  21.             {  
  22.                 {"Name","Sanwar"},  
  23.                 {"City","Jaipur"},  
  24.                 {"Age","23"},  
  25.                 {"Department","Software Development"},  
  26.                 {"Technology","Dot Net"}  
  27.             };  
  28.             collection.InsertOneAsync(Emp);  
  29.             Console.WriteLine("Press Enter");  
  30.             Console.ReadLine();  
  31.         }  
  32.     }  
  33. }  

Run the program and check in the database. The data is entered into EmployeeDetails.

Insert Operation in MonGoDB
In this article, we learned how we can use Robo 3T tool to insert data in MongoDB database using C# Console Application.