Insert Details Into Bson Document Using C# And MongoDB

To insert the details into BsonDocument at code level we should need to add MongoDB BSON reference to the project references.

Here I have written a code with console application in C# adding the BSON reference in namespace and creating the document as follows.

This tutorial follows .Net framework 4.5 and later

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;

namespace MyAPPConsole
{
    class Program
    {
        public static void Main(string[] args)
        {
            var document = new BsonDocument
            {
                {"EmpName","EmpAge"} //creating the document structure
            };
            document.Add("venki", 23); //Adding details into BsonDocument
            document["Level"] = "level2"; //Adding additional column

            var arr = new BsonArray();//creating the array
            arr.Add(new BsonDocument("RefferedEMP", "James")); //adding data to array
            document.Add("Hired", arr); // adding the array to document.

            Console.WriteLine(document);
            Console.ReadLine();
        }
    
    }
}