The Beauty of Mongo
MongoDB is sexy. Sexy as hell.
This isn’t the first or the best intro to Mongo+.NET, but I hope this one can show you how it finally solves the object-relational disconnect with an easy to follow example.
Let’s start with the model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#region prologue // Charles Chen #endregion using System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; namespace MongoAssessmentsTest { public class Assessment { private DateTime _created; private string _description; [BsonId] private ObjectId _id; private List<ContentItem> _items; private DateTime _lastUpdated; private string _ownerId; private List<string> _tags; private string _title; public ObjectId Id { get { return _id; } set { _id = value; } } public string OwnerId { get { return _ownerId; } set { _ownerId = value; } } public string Title { get { return _title; } set { _title = value; } } public string Description { get { return _description; } set { _description = value; } } public List<string> Tags { get { return _tags; } set { _tags = value; } } public DateTime Created { get { return _created; } set { _created = value; } } public DateTime LastUpdated { get { return _lastUpdated; } set { _lastUpdated = value; } } public List<ContentItem> Items { get { return _items; } set { _items = value; } } } public class ContentItem { private List<Question> _questions; private string _text; public string Text { get { return _text; } set { _text = value; } } public List<Question> Questions { get { return _questions; } set { _questions = value; } } } [BsonKnownTypes(typeof(CheckboxQuestion), typeof(RadioQuestion), typeof(SelectQuestion), typeof(FreeTextQuestion))] public class Question { private int _order; private int _points; private string _text; public string Text { get { return _text; } set { _text = value; } } public int Order { get { return _order; } set { _order = value; } } public int Points { get { return _points; } set { _points = value; } } } public class CheckboxQuestion : Question { private List<string> _answers; private List<string> _choices; public List<string> Choices { get { return _choices; } set { _choices = value; } } public List<string> Answers { get { return _answers; } set { _answers = value; } } } public class SelectQuestion : Question { private string _answer; private List<string> _choices; public List<string> Choices { get { return _choices; } set { _choices = value; } } public string Answer { get { return _answer; } set { _answer = value; } } } public class RadioQuestion : SelectQuestion { } public class FreeTextQuestion : Question { private List<string> _acceptableAnswers; private string _isAnswerCaseSensitive; public string IsAnswerCaseSensitive { get { return _isAnswerCaseSensitive; } set { _isAnswerCaseSensitive = value; } } public List<string> AcceptableAnswers { get { return _acceptableAnswers; } set { _acceptableAnswers = value; } } } } |
The beauty of it is the absolute simplicity of working with this model. Aside from a few attributes, there’s not much thought that needs to be given to collections and inheritance hierarchies (though there are additional attributes and classes that can be used to control how these are stored if so desired). I love it because this approach keeps your domain models clean.
How do we interact with this model?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#region prologue // Charles Chen #endregion using System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; namespace MongoAssessmentsTest { internal class Program { private static void Main(string[] args) { Program program = new Program(); program.Run(); } private void Run() { MongoServer server = MongoServer.Create(); // Uses default connection options to localhost MongoDatabase database = server.GetDatabase("assessments_db"); if (!database.CollectionExists("assessments")) { CommandResult result = database.CreateCollection("assessments"); if (!result.Ok) { Console.Out.WriteLine(result.ErrorMessage); return; } } MongoCollection<Assessment> assessments = database.GetCollection<Assessment>("assessments"); if (assessments.FindOne(Query.EQ("Title", "Sample 1")) == null) { // Insert an assessment. Assessment a = new Assessment { Created = DateTime.Now, Description = "A sample assessment", Title = "Sample 1", Items = new List<ContentItem> { new ContentItem { Questions = new List<Question> { new SelectQuestion { Text = "Who was the first president of the United States?", Answer = "George Washington", Choices = new List<string> { "George Washington", "Abraham Lincoln", "John Adams" } }, new CheckboxQuestion { Text = "Which of these is NOT a former US President?", Answers = new List<string> { "Benjamin Franklin", "Hillary Clinton" }, Choices = new List<string> { "Benjamin Franklin", "Bill Clinton", "Hillary Clinton", "Andrew Jackson", "James Garfield" } } } } } }; assessments.Insert(a); } // Get it as BSON - great for writing straight to a web page BsonDocument a1 = assessments.FindOneAs<BsonDocument>(Query.EQ("Title", "Sample 1")); Console.Out.WriteLine(a1); // Get it as an object - great if you want to work with it on the server. Assessment a2 = assessments.FindOneAs<Assessment>(Query.EQ("Title", "Sample 1")); Console.Out.WriteLine(a2.Title); } } } |
Brilliantly simple. Just fire up mongod.exe and you’re ready to rock and roll. Download the example from here: MongoAssessmentsTest.zip