using System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using NUnit.Framework; namespace StackWave.Mongo { public class TestDeserializeNestedStuff2 { public class Stuff { public ObjectId Id { get; set; } public string Name { get; set; } public int Value { get; set; } public object[] Array { get; set; } public IDictionary ExtraStuff { get; set; } } [Test] public void Test() { var stuff = new Dictionary { { "Id", Guid.NewGuid().ToString() }, { "Name", "Test Item" }, { "Value", 1 }, { "Array", new object[] { false, 1, "2" } }, { "ExtraStuff", new Dictionary { { "Key", "Value" }, { "Array", new object[] { "C", "S", "V" } }, { "DeeplyNested", new Dictionary { { "Name", "Sub Item" } } } } } }; var concreteStuff = new Stuff { Name = "Concrete Item", Value = 1, Array = new object[] { false, 1, "2" }, ExtraStuff = new Dictionary { { "Key", "Value" }, { "Array", new object[] { "C", "S", "V" } }, { "DeeplyNested", new Dictionary { { "Name", "Sub Item" } } } } }; var client = new MongoClient("mongodb://localhost/"); var server = client.GetServer(); server.DropDatabase("testdb"); var db = server.GetDatabase("testdb"); var collection = db.GetCollection("testCollection"); //// Save the dictionary //collection.Save(BsonDocument.Create(stuff)); //var persistent = collection.FindOne(Query.EQ("Name", BsonValue.Create("Test Item"))); //// Update the dictionary's extra stuff (with the same stuff) //collection.Update(Query.EQ("Name", BsonValue.Create("Test Item")), new UpdateDocument(new Dictionary { { "$set", new Dictionary { { "ExtraStuff", new Dictionary { { "Key", "Value" }, { "Array", new object[] { "C", "S", "V" } }, { "DeeplyNested", new Dictionary { { "Name", "Sub Item" } } } } } } } })); //persistent = collection.FindOne(Query.EQ("Name", BsonValue.Create("Test Item"))); // Save the class collection.Save(concreteStuff); var existing = collection.FindOneAs(Query.EQ(x => x.Name, "Concrete Item")); // Update the class's extra stuff (also with the same stuff) var extraStuff = new Dictionary { { "Key", "Value" }, { "Array", new object[] { "C", "S", "V" } }, { "DeeplyNested", new Dictionary { { "Name", "Sub Item" } } } }; collection.Update( Query.EQ(x => x.Name, "Concrete Item"), Update.Set(x => x.ExtraStuff, extraStuff)); existing = collection.FindOneAs(Query.EQ(x => x.Name, "Concrete Item")); } } }