Ignoring Properties inside Composite Property with BsonIgnore

47 views
Skip to first unread message

Bharani Dharan

unread,
Mar 26, 2015, 2:10:39 AM3/26/15
to mongod...@googlegroups.com
I uses the below to code to ignore some property inside a class using BsonIgnore. But it is ignoring the total object.

public class User { public string Username { get; set; } public string Password { get; set; } [BsonIgnore,JsonProperty(PropertyName = "CreateDate")] public ICollection<Role> Roles { get; set; } } public class Role { public int RoleId {get; set;} public string RoleName { get; set; } public DateTime CreateDate { get; set;} }

I have 2 question.

  1. How to ignore only certain properties inside a class? I should not use BsonIgnore directly inside Role class.
  2. How to ignore multiple properties? Something like below.
Code:

[BsonIgnore,JsonProperty(PropertyName = "CreateDate")] [BsonIgnore,JsonProperty(PropertyName = "RoleId")] public ICollection<Role> Roles { get; set; }

Craig Wilson

unread,
Mar 26, 2015, 8:13:57 AM3/26/15
to mongod...@googlegroups.com
Hi Bharani,

First, JsonProperty is not a MongoDB attribute.

To answer your question, you add the attribute to the composite type's properties you wish to ignore. You can also use code to register a class map for the nested composite type and indicate which properties to ignore that way.  See the documentation here: http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/.
Reply all
Reply to author
Forward
0 new messages