Sorting an array containing primitives does not work

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      When pushing-with-sort into an array in a document, if that array contains primitives rather than sub-documents, the sort is not being applied.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      3.4.2

      How to Reproduce

      Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

      The following a simple console app that demonstrates the issue. It assumes that MongoDB.Driver 3.4.2 is installed.

      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Attributes;
      using MongoDB.Bson.Serialization.IdGenerators;
      using MongoDB.Driver;
      
      namespace MongoChecks;
      
      internal class Program
      {
          private static async Task Main(string[] args)
          {
              MongoClient client = new("mongodb://localhost:27017/?replicaSet=rs0");
              var db = client.GetDatabase("MongoTests");
              var collection = db.GetCollection<Document>("Docs");
      
              Document template = new Document
              {
                  Values = [4, 2, 6],
                  SubDocuments = [
                      new(){ Value = 4 },
                      new(){ Value = 2 },
                      new(){ Value = 6 },
                      ]
              };
              Document doc = template with { };
              Document doc2 = template with { };
      
              await collection.InsertOneAsync(doc);
      
              await collection.UpdateOneAsync(
                  dbDoc => dbDoc.Id == doc.Id,
                  Builders<Document>.Update.PushEach(
                      doc => doc.Values,
                      [1, 5, 3],
                      sort: Builders<int>.Sort.Ascending(val => val)));
      
              var dbDocs = await collection.FindAsync(
                  dbDoc => dbDoc.Id == doc.Id);
      
              var res = await dbDocs.ToListAsync();
              Console.WriteLine("Push-sorted values: " + string.Join(", ", res.Single().Values));
      
              await collection.UpdateOneAsync(
                  dbDoc => dbDoc.Id == doc.Id,
                  Builders<Document>.Update.PushEach(
                      doc => doc.SubDocuments,
                      [
                          new SubDocument() { Value = 1},
                          new SubDocument() { Value = 5},
                          new SubDocument() { Value = 3}],
                      sort: Builders<SubDocument>.Sort.Ascending(subDoc => subDoc.Value)));
      
              dbDocs = await collection.FindAsync(
                  dbDoc => dbDoc.Id == doc.Id);
      
              res = await dbDocs.ToListAsync();
              Console.WriteLine("Push sorted sub-docs: " + string.Join(", ", res.Single().SubDocuments));
      
              await collection.InsertOneAsync(doc2);
      
              await collection.UpdateOneAsync(
                  dbDoc => dbDoc.Id == doc2.Id,
                  Builders<Document>.Update.PushEach(
                      doc => doc.Values,
                      [],
                      sort: Builders<int>.Sort.Ascending(val => val)));
      
              dbDocs = await collection.FindAsync(
                  dbDoc => dbDoc.Id == doc2.Id);
      
              res = await dbDocs.ToListAsync();
              Console.WriteLine("Sorted values: " + string.Join(", ", res.Single().Values));
      
              await collection.UpdateOneAsync(
                  dbDoc => dbDoc.Id == doc2.Id,
                  Builders<Document>.Update.PushEach(
                      doc => doc.SubDocuments,
                      [],
                      sort: Builders<SubDocument>.Sort.Ascending(val => val.Value)));
      
              dbDocs = await collection.FindAsync(
                  dbDoc => dbDoc.Id == doc2.Id);
      
              res = await dbDocs.ToListAsync();
              Console.WriteLine("Sorted sub-docs: " + string.Join(", ", res.Single().SubDocuments));
          }
      }
      
      internal record Document
      {
          [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
          public ObjectId Id { get; set; }    public List<int> Values { get; set; } = [];
          public List<SubDocument> SubDocuments { get; set; } = [];
      }
      
      internal record SubDocument
      {
          public int Value { get; set; }
      } 
      

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

              Assignee:
              Adelin Mbida Owona
              Reporter:
              Shlomi Borovitz
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated: