Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created April 21, 2010 21:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottwater/374418 to your computer and use it in GitHub Desktop.
Save scottwater/374418 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using MongoDB.Driver;
namespace DynoMongo
{
public class DynoCollection
{
private IMongoCollection _col;
public DynoCollection(IMongoCollection col)
{
_col = col;
}
public IMongoCollection Collection
{
get { return _col; }
}
public void Insert(DynoDocument dyDoc)
{
_col.Insert(dyDoc.ToDocument());
}
public void Update(DynoDocument dyDoc, DynoDocument selector=null, bool safemode=false)
{
if (selector == null)
_col.Update(dyDoc.ToDocument(), safemode);
else
_col.Update(dyDoc.ToDocument(), selector.ToDocument(), safemode);
}
public void Update(DynoDocument dyDoc, DynoDocument selector, UpdateFlags flags=0, bool safemode=false)
{
_col.Update(dyDoc.ToDocument(), selector.ToDocument(), flags, safemode);
}
public void UpdateAll(DynoDocument dyDoc, DynoDocument selector, bool safemode=false)
{
_col.UpdateAll(dyDoc.ToDocument(), selector.ToDocument(), safemode);
}
public IEnumerable<dynamic> FindAll()
{
return _col.FindAll().DynoDocuments();
}
public IEnumerable<dynamic> Find(string where)
{
return _col.Find(where).DynoDocuments();
}
public IEnumerable<dynamic> Find(DynoDocument spec, int limit = 0, int skip = 0)
{
return _col.Find(spec.ToDocument(), limit, skip).DynoDocuments();
}
public IEnumerable<dynamic> Find(DynoDocument spec, int limit=0, int skip=0, params string[] columns)
{
Document docFilter = new Document();
foreach (string column in columns)
docFilter[column] = true;
return _col.Find(spec.ToDocument(), limit, skip, docFilter).DynoDocuments();
}
public dynamic FindOne(DynoDocument spec)
{
var doc = _col.FindOne(spec.ToDocument());
return new DynoDocument(doc);
}
}
}
using System.Collections.Generic;
using System.Dynamic;
using MongoDB.Driver;
namespace DynoMongo
{
public class DynoDocument : DynamicObject
{
private Document container;
public DynoDocument():this(null){}
//Need to ensure all sub-documents are converted into
//sub-dynodocuments
public DynoDocument(Document doc)
{
container = new Document();
if (doc != null)
{
foreach (string key in doc.Keys)
{
object obj = doc[key];
Document d = obj as Document;
if (d != null)
{
container[key] = new DynoDocument(d);
}
if (!container.Contains(key))
{
var da = obj as IEnumerable<Document>;
if (da != null)
{
var list = new List<DynoDocument>();
foreach (Document child_doc in da)
list.Add(new DynoDocument(child_doc));
container[key] = list.ToArray();
}
}
if (!container.Contains(key))
container[key] = obj;
}
}
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (container.Contains(binder.Name))
{
result = container[binder.Name];
return true;
}
return base.TryGetMember(binder, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
container[binder.Name] = value;
return true;
}
//Not as simple as returning container. Need to ensure
//sub-dynodocuments have .ToDocument() invoked.
public Document ToDocument()
{
Document doc = new Document();
foreach (string key in container.Keys)
{
object obj = container[key];
var dynoDocs = obj as IEnumerable<DynoDocument>;
if (dynoDocs != null)
{
var list = new List<Document>();
foreach (DynoDocument idoc in dynoDocs)
{
list.Add(idoc.ToDocument());
}
doc[key] = list.ToArray();
}
else
{
DynoDocument dd = obj as DynoDocument;
doc[key] = dd == null ? obj : dd.ToDocument();
}
}
return doc;
}
}
}
using System.Collections.Generic;
using System.Dynamic;
using MongoDB.Driver;
namespace DynoMongo
{
public static class ICursorExt
{
public static IEnumerable<DynamicObject> DynoDocuments(this ICursor cursor)
{
foreach (var doc in cursor.Documents)
yield return new DynoDocument(doc);
}
}
public static class DatabaseExt
{
public static DynoCollection GetDynoCollection(this Database db, string name)
{
return new DynoCollection(db.GetCollection(name));
}
}
}
using (var m = new Mongo())
{
var db = m["mongocsharp"];
var col = db.GetDynoCollection("sample");
m.Connect();
dynamic newDoc = new DynoDocument();
newDoc.title = "Dyno Collections Rock";
dynamic child1 = new DynoDocument();
child1.name = "I am a child 1";
dynamic child2 = new DynoDocument();
child2.name = "I am a child 2";
newDoc.children = new List<DynoDocument> { child1, child2 };
col.Insert(newDoc);
dynamic query = new DynoDocument();
IEnumerable<dynamic> results = col.FindAll();
foreach (var d in results)
{
Console.WriteLine(d.title);
Console.WriteLine(d.children[0].name);
}
m.Disconnect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment