0

I'm trying to work on an app that has two main domain entities: Entry and Category, and I'm going to use mongodb. I will have one collection for Entries and one for Categories. Entries collection may have thousands of hundreds of documents, and Categories hundreds.

In my view, I want to show all entries' information and their category name and color. I'd like to keep their category id so I can update the affected entries if a category name or color changes. So I'd like to have a document like that:

{
  "_id": 123456,
  "date": '2018-08-15',
  "description": "Some entry description"
  ....
  category:
  {
    "id": 123,
    "name": "My category",
    "color:" "blue"
  }

The problem is that Category has lots more of properties, so my document ends up like that:

{
    "_id": 123456,
    "date": '2018-08-15',
    "description": "Some entry description"
     ....
    category:
    {
       "id": 123,
        "name": "My category",
        "color": "blue",
        "otherProp": "a",
        "anotherProp": "b",
        "differentProp": "c"
    }
}

I tried to use BsonClassMap.RegisterClassMap to map only some properties of Category for Entry collection but that seems not possible. If I ignore some Category property, categories` collection will not have these ignored items as well.

Should I work on a different model representation like bellow or create new entities to save the information as I want (so my repository won't persist Entry but will persistEntryDataObject)?

public class Entry {
   public string Description { get; set; }
   ...
   public Category Category { get; set; }
}

public class Category { 
   public string Name { get; set; }
   public string Color { get; set; }
}

public class CategoryExtraInformation {
   public string OtherProp { get; set; }
   public string AnotherProp { get; set; }
   public string DifferentProp { get; set; }
   public Category Category { get; set; }
}
4
  • There are Mongodb attributes you can use, like [Ignore] and ignoring extra properties. api.mongodb.com/csharp/1.5/html/… Commented Aug 15, 2018 at 14:43
  • Yes, but I still need a Category collection. If I ignore properties on Category, that will work for my Entry document, but the categories collection will be incomplete. Commented Aug 15, 2018 at 14:48
  • One option would be to use custom serializers which is a little painful and seems like an overkill for this problem. Some slightly dated documentation which, however, is still largely valid for the latest version of the C# driver: mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/… Commented Aug 15, 2018 at 21:04
  • I just found that: blog.markstarkman.com/blog/2011/09/22/… I think it may help me. I will try it. Commented Aug 15, 2018 at 21:27

1 Answer 1

1

I am facing a similar issue right now. The decorators (ie [Ignore]) also do not properly address my use case because they impact all places where that class is used.

The solution I am using is to provide helper functions (and/or constructors) which create the class with the proper subset of properties.

For example, setup a constructor that builds a new object that contains only the properties needed for the embedded instance...Let's say you have a "full blown" instance of the Category class called category. You now want to update or create an Entry instance. If you have a limited scope constructor

public Category(int ID, string name, string color)
{
    id = ID;
    Name = name;
    Color = color
}

You can then call it to create a new Category object that has the limited fields as follows:

var categoryLimited = new Category(category.id, category.Name, category.Color);

Now do the save or update operation in Mongo using categoryLimited. The MongoDB record will only contain the desired attributes. Obviously, this approach will be limited to classes that do not have default values or mandatory fields within the "extra attributes".

Best of luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.