Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.