Created
February 2, 2014 16:39
Revisions
-
kaushikraj created this gist
Feb 2, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ using MongoDB.Bson; using MongoDB.Driver; using System; using System.Threading; using System.IO; namespace ConsoleApplication1 { public class Stamp { public long Tick; } class Program { public const string connectionString = "mongodb://testdbuser:[email protected]:53117,flop.mongolab.com:54117/testdb?replicaSet=rs-flip-flop"; public const int Max = 100; public const int WaitTime = 3; public static void SaveData() { int i = 0; MongoClient client = new MongoClient(connectionString); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("testdb"); string collectionName = GetRandomCollectionName(); MongoCollection collection = database.GetCollection(collectionName); try { while (i < Max) { try { Console.Write("Insert attempt : {0}", i + 1); collection.Insert<Stamp>(new Stamp() { Tick = DateTime.Now.Ticks }); Console.WriteLine(", done."); } catch (Exception ese) { Console.WriteLine("exception : {0}.", ese.Message); Console.WriteLine("Sleeping for {0} seconds before attempting again", WaitTime); Thread.Sleep(WaitTime * 1000); continue; } i++; Thread.Sleep(1000); } } finally { collection.Drop(); } } private static string GetRandomCollectionName() { return string.Format("col-{0}", DateTime.Now.Ticks) ; } static void Main(string[] args) { try { SaveData(); } catch (Exception exception) { Console.WriteLine(exception); } } } }