Discussion:
[mongodb-user] Ignoring Properties inside Composite Property with BsonIgnore
Bharani Dharan
2015-03-26 01:07:49 UTC
Permalink
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; }
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/e4afc79b-d716-47b7-9a37-ef4b4fbe2561%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Craig Wilson
2015-03-26 12:13:57 UTC
Permalink
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/.
Post by Bharani Dharan
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.
[BsonIgnore,JsonProperty(PropertyName = "CreateDate")] [BsonIgnore,JsonProperty(PropertyName = "RoleId")] public ICollection<Role> Roles { get; set; }
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/7fa48ecd-840e-42a0-8302-46719fbe3d36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...