BsonElementAttribute name for nested document

158 views
Skip to first unread message

Chip Marklar

unread,
Oct 18, 2012, 7:17:21 PM10/18/12
to mongodb...@googlegroups.com
With a document structure
{_id:"abc",value:{x:1,y:2,z:3}}
i'd like to populate a poco:

public class{
string Id{get;set;}
int x:{get;set;}
int y:{get;set;}
int z:{get;set;}
}

Is it supported to place the BsonElementAttribute with dotted notation?
[BsonElement("value.x")]
int x:{get;set;}

It compiles fine, but doesn't populate x

Robert Stam

unread,
Oct 19, 2012, 2:23:56 PM10/19/12
to mongodb...@googlegroups.com
[BsonElement] can only be used to change the name of the element, not the structure of the resulting BSON document.

To get the BSON document you want you would have to change your class to something like:

    public class C
    {
        public string Id { get; set; }
        public XYZ value { get; set; }
    }

    public class XYZ
    {
        public int x { get; set; }
        public int y { get; set; }
        public int z { get; set; }

Chip Marklar

unread,
Oct 19, 2012, 2:47:21 PM10/19/12
to mongodb...@googlegroups.com
Thanks!

That was, in fact, my code (since the attribute didn't do nested elements).

I was wondering if there was an alternate way to specify serialization of nested object in some what that would "pull" or "flatten" nested mongo doc into a flat poco.
The full structure class with 1-1 mapping to the doc structure is straightforward enough and satisfactory.

But for cases where I'd want to report flat object, the extra layer of translation seems heavier because a 1-1 class would be generated first (many of them) and then each would be in-memory projected (by me) to my final flat subset .
If one wanted to go BSON direct,one would lose the nice IQueryable expressiveness.
Reply all
Reply to author
Forward
0 new messages