Mapping parent / child class to one (same) document

58 views
Skip to first unread message

Alex Brown

unread,
Jul 6, 2012, 12:53:17 PM7/6/12
to mongodb...@googlegroups.com
Currently, I have one large class that looks like:

   public class Result
    {
        public virtual int ResultId { get; set; }
        public virtual DateTime ResultDate { get; set; }
        public virtual string ResultVenue { get; set; }
        public virtual string DisciplineCode { get; set; }
        public virtual string DisciplineName { get; set; }
        public virtual string DisciplineDescription { get; set; }
       //other properties removed for brevity
   }

This is stored as a flat document in the result collection in my mongodb db.

Is it possible to break this up, and create a Discipline class such as:

   public class Discipline
    {
        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
   }

And change my Result class to:

   public class Result
    {
        public virtual int ResultId { get; set; }
        public virtual DateTime ResultDate { get; set; }
        public virtual Discipline Discipline { get; set; }
       //other properties
   }

But still keep this mapped as ONE document?




craiggwilson

unread,
Jul 8, 2012, 4:07:11 PM7/8/12
to mongodb...@googlegroups.com
Absolutely.  MongoDB can store anything that JSON can store.  We will not flatten it in the datastore to match your first class, but this would serialize like the below document.

{
  ResultId : 1,
  ResultDate, Date("..."),
  Discipline: 
  {
    Code : "123",
    Name: "Awesome",
    Description: "Some awesome discipline",
  }
  ... other properties

Alex Brown

unread,
Jul 9, 2012, 11:26:07 AM7/9/12
to mongodb...@googlegroups.com
Cool,
I was hoping it was like that!
Thanks
Reply all
Reply to author
Forward
0 new messages