- 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".
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.
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.
Step 4:
Now in the main program paste the following code:
- 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;
- }
- }
- }
- var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson());
- nextMessages.InsertBatch(netxMessageBatch);
- DataProvider dp = new DataProvider();
- dp.InsertBatch();
- Console.WriteLine();
So the
complete code is:
When you run the application the results will be as shown in the following
figure:
So in this article, we have seen how to use the insert batch statement with a Mongo database.
- 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();
- }
- }
- }


ambati ambatiPosted Jul 8, 2011, 5:41 AM
This code take high cpu usage (" var netxMessageBatch = BsonSerializer.Deserialize<BsonDocument[]>(new NextMessage().GetAddress(20).ToJson()); ") Can you provide alternative solution for me