Insert Details Into Bson Document Using C# And MongoDB - OmIndia

Teach To India

Friday, August 10, 2018

Insert Details Into Bson Document Using C# And MongoDB

Insert Details Into Bson Document Using C# And MongoDB


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();        }        }}


Creating Nested BSON Documents
Nested BSON documents are created by setting the value of an element to a BSON document. For example:
BsonDocument nested = new BsonDocument {
    { "name", "John Doe" },
    { "address", new BsonDocument {
        { "street", "123 Main St." },
        { "city", "Centerville" },
        { "state", "PA" },
        { "zip", 12345}
    }}
};
                                         OR                                                                                    
        var con = ConfigurationManager.ConnectionStrings["MongoConnectionNew"].ConnectionString;
        MongoClient Client_ = new MongoClient(con);
        MongoServer server = Client_.GetServer();
        MongoDatabase data=server.GetDatabase("CRUD");
        MongoCollection col = data.GetCollection("CRUDCOL");
        var rr = new BsonArray();
        rr.Add(new BsonDocument("Name", "Hariom"));
        rr.Add(new BsonDocument("Name", "Kishor"));
        rr.Add(new BsonDocument("Name", "Naveen"));
        col.InsertBatch(rr);







No comments:

Post a Comment

Comments

Popular