-
-
Save chrisckchang/8828185 to your computer and use it in GitHub Desktop.
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 characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment