Skip to content

Instantly share code, notes, and snippets.

@kenegozi
Created August 28, 2012 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenegozi/3504325 to your computer and use it in GitHub Desktop.
Save kenegozi/3504325 to your computer and use it in GitHub Desktop.
Accessing mongodb using the official c# driver without types
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Wrappers;
using Newtonsoft.Json;
namespace Mongothingies {
class Program {
static void Main() {
var server = MongoServer.Create();
var db = server.GetDatabase("mongothingies");
var col = db.GetCollection("things");
col.Insert(ToBsonDoc(new { age = 34, name = "Ken" }));
col.Insert(ToBsonDoc(new { age = 35, name = "Future Ken" }));
var x = col.FindOne(QueryWrapper.Create(new { age = 35 }));
Console.WriteLine(x["name"]);
}
static BsonDocument ToBsonDoc(object obj) {
return BsonDocument.Parse(JsonConvert.SerializeObject(obj));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment