Created
August 28, 2012 21:02
-
-
Save kenegozi/3504325 to your computer and use it in GitHub Desktop.
Accessing mongodb using the official c# driver without types
This file contains 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 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