Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1856

SetOrder doesn't seem to order the field in the document

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4.1
    • Affects Version/s: 2.0
    • Component/s: Serialization
    • None

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            peter.garafano@mongodb.com Peter Garafano (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: