Skip to content

Instantly share code, notes, and snippets.

@jeffsaracco
Created October 17, 2011 00:09
Show Gist options
  • Save jeffsaracco/1291625 to your computer and use it in GitHub Desktop.
Save jeffsaracco/1291625 to your computer and use it in GitHub Desktop.
Fast Inserts and Updates into MongoDB
private const string host = "mongodb://localhost/";
static void Main(string[] args)
{
MongoServer server = MongoServer.Create(host);
MongoDatabase test = server.GetDatabase("testDB");
List<int> list = Enumerable.Range(0, 100000).ToList();
using (server.RequestStart(test))
{
MongoCollection coll = test.GetCollection("testCollection");
server.IndexCache.Add(coll, "_id_");
List<BsonValue> b = list.Select(l => BsonValue.Create(l)).ToList();
var q = Query.In("_id", b);
var found = coll.FindAs<BsonDocument>(q);
found.SetFields(new[] {"_id"});
DateTime start = DateTime.Now;
var f = found.AsParallel().Select(fo => fo["_id"].AsInt32).ToList();
Console.WriteLine(string.Format("Found {0} docs", f.Count));
List<BsonDocument> documents = list.Except(f).Select(item => new BsonDocument { { "_id", item }, { "value", list[item] + list[item] } }).ToList();
var updateDocs = list.Intersect(f);
Console.WriteLine(string.Format("starting threads"));
ThreadStart ts = () =>
{
documents = documents.AsParallel().Where(d => d != null).ToList();
Console.WriteLine(string.Format("inserting {0} documents",documents.Count));
coll.InsertBatch(documents, SafeMode.False);
};
Thread t = new Thread(ts);
ThreadStart update = () => Parallel.ForEach(updateDocs, i =>
{
var query = new QueryDocument("_id", i);
coll.Update(query,
Update.Set("value", i+i),
SafeMode.False);
});
Thread up = new Thread(update);
up.Start();
t.Start();
t.Join();
up.Join();
DateTime end = DateTime.Now;
Console.WriteLine(string.Format("Wrote {0} lines in {1}ms", list.Count, end.Subtract(start).TotalMilliseconds));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment