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

Grouping requires Sort to work correctly

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.0
    • API
    • None
    • c# VS2013 Local mongodb

    Description

      Given class hierarchy

      public class Branch : Aggregate
      {
          public IEnumerable<LocalizableText> Description { get; set; }
          public ObjectId PartnerId { get; set; }
          public double Latitude { get; set; }
          public double Longitude { get; set; }
          public string Timetable { get; set; }
          public IEnumerable<Discount> Discounts { get; set; }
          public IEnumerable<Category> Categories { get; set; }
          public IEnumerable<Phone> Phones { get; set; }
          public byte[] Icon { get; set; }
          public byte[] Image { get; set; }
      }
       
      public abstract class Aggregate : Entity
      {
          public ObjectId Id { get; set; }
      }
       
      public abstract class Entity
      {
          public IEnumerable<LocalizableText> Name { get; set; }
      }
      

      Given class mappings

      public void RegisterClassMaps()
              {
                  if (!BsonClassMap.IsClassMapRegistered(typeof (Entity)))
                  {
                      BsonClassMap.RegisterClassMap<Entity>(cm =>
                      {
                          cm.SetIsRootClass(true);
                          cm.MapProperty(e => e.Name);
                          //cm.SetDiscriminator(typeof(Entity).Name);
                          cm.AddKnownType(typeof (Aggregate));
                          cm.AddKnownType(typeof (Branch));
       
                      });
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof (LocalizableText)))
                  {
                      BsonClassMap.RegisterClassMap<LocalizableText>();
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof (Discount)))
                  {
                      BsonClassMap.RegisterClassMap<Discount>();
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof (Category)))
                  {
                      BsonClassMap.RegisterClassMap<Category>();
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof (Phone)))
                  {
                      BsonClassMap.RegisterClassMap<Phone>();
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof (Aggregate)))
                  {
                      BsonClassMap.RegisterClassMap<Aggregate>(cm =>
                      {
                          //cm.SetDiscriminator(typeof(Aggregate).Name);
                          cm.MapIdMember(a => a.Id);
                          cm.IdMemberMap.SetIdGenerator(new StringObjectIdGenerator())
                              .SetSerializer(new StringSerializer(BsonType.ObjectId));
                          cm.AddKnownType(typeof (Branch));
                      });
                  }
                  if (!BsonClassMap.IsClassMapRegistered(typeof(Branch)))
                  {
                      BsonClassMap.RegisterClassMap<Branch>(cm =>
                      {
                          cm.MapProperty(b => b.Description);
                          cm.MapProperty(b => b.Address);
                          cm.MapProperty(b => b.Discounts);
                          cm.MapProperty(b => b.Icon);
                          cm.MapProperty(b => b.Image);
                          cm.MapProperty(b => b.Location);
                          cm.MapProperty(b => b.Url);
                          cm.MapMember(b => b.PartnerId).SetSerializer(new StringSerializer(BsonType.ObjectId));
                          cm.MapProperty(b => b.Phones);
                          cm.MapProperty(b => b.Timetable);
                          cm.MapProperty(b => b.Categories);
                          //cm.SetDiscriminator(typeof(Branch).Name);
                      });
                  }
              }
      

      When I do the groupping Sort is required in pipeline before the Group or groupping produces wrong results

                  var sortBuilder = new SortDefinitionBuilder<Branch>().Ascending(b => b.Id).Ascending(b => b.PartnerId);
                  var groups = await Collection.Aggregate().Sort(sortBuilder).Group(x => x.PartnerId, g => new 
                  {
                                                                                      PartnerId = g.Key,
                                                                                      g.First(x => x.Id == x.PartnerId).Name,
                                                                                      g.First(x => x.Id == x.PartnerId).Address,
                                                                                      g.First(x => x.Id == x.PartnerId).Description,
                                                                                      g.First(x => x.Id == x.PartnerId).Icon,
                                                                                      g.First(x => x.Id == x.PartnerId).Image,
                                                                                      g.First(x => x.Id == x.PartnerId).Phones,
                                                                                      g.First(x => x.Id == x.PartnerId).Timetable,
                                                                                      g.First(x => x.Id == x.PartnerId).Categories,
                                                                                      g.First(x => x.Id == x.PartnerId).Url,
                                                                                      g.First(x => x.Id == x.PartnerId).Discounts,
                                                                                      g.First(x => x.Id == x.PartnerId).Location
                                                                                  }).ToListAsync();
      }
      

      Expected: Sort not required to do the Group

      Notes: I'm 99% sure First statement works here as expected.

      Attachments

        Activity

          People

            craig.wilson@mongodb.com Craig Wilson
            Cortlendt Dmitry Kuznetsov [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: