SIGN UP MEMBER LOGIN:    
ARTICLE

How to Use InsertBatch With BSon Serialization in a Mongo database in C#

Posted by Shirsendu Nandi Articles | ADO.NET in C# May 18, 2011
In this article I will describe the use of the insert batch statement in a Mongo database for inserting a large amount of data.
Reader Level:
Download Files:
 


The Insert batch statement is used in a Mongo database for inserting a large amount of json data or normal data into a Mongo database. Here I will create an application which will insert a batch of dictionary data into the Mongo database using insert batch.

Step 1:

Create a console application named "InserBatchforMOngodb".

Step 2:

Now create a class named "NExtMessage".

public class NextMessage
        { 
            public List<Dictionary<string, object>> GetAddress(int MessageId)
            {
                var AddressList = new List<Dictionary<string, object>>();
 
                for (int i = MessageId; i < MessageId + 9; i++)
                {
                    Dictionary<string, object> message = new Dictionary<string, object>();
                    message.Add("Id", i);
                    message.Add("DateCreated", DateTime.Today.AddDays(i));
                    message.Add("MessageType", "SAS");
                    message.Add("AlertName", "Airtel" + i.ToString());
                    AddressList.Add(message);
                }
                return AddressList;
            }
        }

Here you see I passed the value into the dictionary.

Step 3:

Now create a class named "DataProvider".

    public class
DataProvider
    {
        ///
<summary>
        ///
        /// </summary>
        MongoCollection<BsonDocument> nextMessages, om9Messages;
        ///
<summary>
        ///
        /// </summary>
        MongoServer server;
        ///
<summary>
        ///
        /// </summary>
        MongoDatabase oneConsoleDB;
 
        ///
<summary>
        /// Opens the connection.
        /// </summary>
        private void openConnection()
        {
            if (server.State == MongoServerState.Disconnected)
                server.Connect();
 
            oneConsoleDB = server.GetDatabase("AirMessage");
            if (!oneConsoleDB.CollectionExists("NextMessages"))
                oneConsoleDB.CreateCollection("NextMessages", null);
 
            nextMessages = oneConsoleDB.GetCollection("NextMessages");
       
        }
 
        public DataProvider()
        {
            string connectionString = "mongodb://192.168.40.27/?sockettimeout=5m";
            server = MongoServer.Create(connectionString);
 
        }
 
        ///
<summary>
        /// Inserts the batch.
        /// </summary>
        public void InsertBatch()
        {
            openConnection();
            nextMessages.RemoveAll();
           
//om9Messages.RemoveAll();
            var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson());
            
           
//var test = netxMessageBatch.Where(t => { t.Elements.Where(k => k.Name.Equals("AlertName") && k.Value == "NetxFM23"); return true; });
            var test = netxMessageBatch.Where(t => t.Elements.ElementAt(3).Value.Equals("NetxFM23"));
 
            nextMessages.InsertBatch(netxMessageBatch);
 
            Console.WriteLine("Inserted successfully");
            server.Disconnect();
            Console.ReadLine();
        }
        public class
NextMessage
        {
 
            public List<Dictionary<string, object>> GetAddress(int MessageId)
            {
                var AddressList = new List<Dictionary<string, object>>();
 
                for (int i = MessageId; i < MessageId + 9; i++)
                {
                    Dictionary<string, object> message = new Dictionary<string, object>();
                    message.Add("Id", i);
                    message.Add("DateCreated", DateTime.Today.AddDays(i));
                    message.Add("MessageType", "SAS");
                    message.Add("AlertName", "Airtel" + i.ToString());
                    AddressList.Add(message);
                }
                return AddressList;
            }
        }
    }

See in the DataProvider method I have written my system IP address. You need to use your own system IP address for connecting the database.
Now see this code. This is the most important code before using the insertbatch statement.

            var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson());

Here whenever we are getting dictionary data from the "
NextMessage" class we have to deserialize it before putting it into the Mongo database.
Now the following code is for saving the dictionary data into the Mongo database.

nextMessages.InsertBatch(netxMessageBatch);

Step 4:

Now in the main program paste the following code:

   
DataProvider dp = new DataProvider();
            dp.InsertBatch();
            Console.WriteLine();

So the complete code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using MongoDB.Driver.Internal;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.DefaultSerializer;
using System.IO;
using mongo = MongoDB;
using MongoDB.Linq;
 
namespace INsertBAtchforMOngoDb
{
    public class
DataProvider
    {
        ///
<summary>
        ///
        /// </summary>
        MongoCollection<BsonDocument> nextMessages, om9Messages;
        ///
<summary>
        ///
        /// </summary>
        MongoServer server;
        ///
<summary>
        ///
        /// </summary>
        MongoDatabase oneConsoleDB;
 
        ///
<summary>
        /// Opens the connection.
        /// </summary>
        private void openConnection()
 
      {
            if (server.State == MongoServerState.Disconnected)
                server.Connect();
 
            oneConsoleDB = server.GetDatabase("AirMessage");
 
            if (!oneConsoleDB.CollectionExists("NextMessages"))
                oneConsoleDB.CreateCollection("NextMessages", null);
 
            nextMessages = oneConsoleDB.GetCollection("NextMessages");       
        }
 
        public DataProvider()
        {
            string connectionString = "mongodb://192.168.40.27/?sockettimeout=5m";
            server = MongoServer.Create(connectionString); 
        }
 
        ///
<summary>
        /// Inserts the batch.
        /// </summary>
        public void InsertBatch()
        {
            openConnection();
            nextMessages.RemoveAll();
           
//om9Messages.RemoveAll();
            var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson());
 
           
//var test = netxMessageBatch.Where(t => { t.Elements.Where(k => k.Name.Equals("AlertName") && k.Value == "NetxFM23"); return true; });
            var test = netxMessageBatch.Where(t => t.Elements.ElementAt(3).Value.Equals("NetxFM23")); 
            nextMessages.InsertBatch(netxMessageBatch);
 
            Console.WriteLine("Inserted successfully");
            server.Disconnect();
            Console.ReadLine();
        }
        public class
NextMessage
        {
            public List<Dictionary<string, object>> GetAddress(int MessageId)
            {
                var AddressList = new List<Dictionary<string, object>>();
 
                for (int i = MessageId; i < MessageId + 9; i++)
                {
                    Dictionary<string, object> message = new Dictionary<string, object>();
                    message.Add("Id", i);
                    message.Add("DateCreated", DateTime.Today.AddDays(i));
                    message.Add("MessageType", "SAS");
                    message.Add("AlertName", "Airtel" + i.ToString());
                    AddressList.Add(message);
                }
                return AddressList;
            }
        } 
    }
    class
Program
    {
        static void Main(string[] args)
        {
            DataProvider dp = new DataProvider();
            dp.InsertBatch();
            Console.WriteLine();
        }
    }   
}


When you run the application the results will be as shown in the following figure:

Untitled-7.gif
Conclusion: So in this article we have seen how to use the insert batch statement with a Mongo database.

Login to add your contents and source code to this article
share this article :
post comment
 

This code take high cpu usage (" var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson()); ") Can you provide alternative solution for me

Posted by ambati ambati Jul 08, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor