[CSHARP-1856] SetOrder doesn't seem to order the field in the document Created: 01/Dec/16  Updated: 19/Dec/16  Resolved: 02/Dec/16

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.0
Fix Version/s: 2.4.1

Type: Bug Priority: Major - P3
Reporter: Peter Garafano (Inactive) Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have a class, Foo, that looks like the following.

public class Foo
{
	public ObjectId Id { get; set; }
	public string A { get; set; }
	public string B { get; set; }
	public string C { get; set; }
}

And I map the class as follows

BsonClassMap.RegisterClassMap<Foo>(cm =>
{
	cm.AutoMap();
	cm.MapMember(c => c.A).SetOrder(3);
	cm.MapMember(c => c.B).SetOrder(2);
	cm.MapMember(c => c.C).SetOrder(1);
});

I expect my document to look like the following

> db.foo.findOne()
{
        "_id" : ObjectId("58407cb1a589ca66b0d0ac48"),
        "C" : "0",
        "B" : "2016",
        "A" : "Jan"
}

But they actually look like this.

> db.foo.findOne()
{
        "_id" : ObjectId("58407bf8a589ca8f5cedb77b"),
        "A" : "Jan",
        "B" : "2016",
        "C" : "0"
}

In order to get the desired order, I have to do the following mapping, making AutoMap last.

BsonClassMap.RegisterClassMap<Foo>(cm =>
{
	cm.MapMember(c => c.A).SetOrder(3);
	cm.MapMember(c => c.B).SetOrder(2);
	cm.MapMember(c => c.C).SetOrder(1);
	cm.AutoMap();
});

These should either be independent of order or we need to be explicit that SetOrder needs to come first.



 Comments   
Comment by Githook User [ 02/Dec/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1856: Calling SetOrder was not working because the sorting was happening too early.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/b4114d2948143f4174485517c819c0566f1b27f4

Comment by Peter Garafano (Inactive) [ 01/Dec/16 ]

Hi Ryan,

I didn't consider the GetMemberMap method. I modified the class map as you suggested, to look like this:

BsonClassMap.RegisterClassMap<Foo>(cm =>
{
	cm.AutoMap();
	cm.GetMemberMap(c => c.A).SetOrder(3);
	cm.GetMemberMap(c => c.B).SetOrder(2);
	cm.GetMemberMap(c => c.C).SetOrder(1);
});

However it still yields the undesired ordering.

> db.foo.findOne()
{
        "_id" : ObjectId("584081dca589ca62c066aa00"),
        "A" : "Jan",
        "B" : "2016",
        "C" : "0"
}

Thanks,
Pete

Comment by Ryan Kuhn [ 01/Dec/16 ]

Have you tried:

cm.AutoMap();
cm.GetMemberMap(c => c.A).SetOrder(3);
cm.GetMemberMap(c => c.B).SetOrder(2);
cm.GetMemberMap(c => c.C).SetOrder(1);

Generated at Wed Feb 07 21:40:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.