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

c# driver throws on aggregate function when the source sequence is empty

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.1
    • Component/s: Linq, Read Operations
    • None
    • Environment:
      c# driver 2.6.1,
      server version 3.4.10
      windows 7 sp1 64 bit

      I have a class:

      class DynamicForm
      {
           public long? FolderId { get; set; } 
           public long PatientSystemId { get; set; }
      
           public DateTime ModifiedDate { get; set; }
      }

       

      I have a function that gets the PatientSystemId as parameter GetDynamicFormsAsync(long patientSystemId)

      I run the following query:

      var query = db.GetCollection<DynamicForm>("dforms").AsQueryable()
         .Where(a => a.PatientSystemId == patientSystemId && a.FolderId == null)
         .MaxAsync(a => a.ModifiedDate);
      var qResult = await query;
      

      When there are documents in the database that meet the filter criteria I do get the Max date as desired.

      However, if there is no document meeting the query criteria then I get a run-time exception:

      System.InvalidOperationException
       HResult=0x80131509
       Message=The source sequence is empty.
       Source=MongoDB.Driver
       StackTrace:
       at MongoDB.Driver.AsyncCursorHelper.<SingleAsync>d__3`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ..... my code
      

      When patientSystemId parameter equals 51, the following query is generated:

      { 
          "aggregate" : "dforms", 
          "pipeline" : [
              { "$match" : { "patientSystemId" : NumberLong(51), "folderId" : null } }, 
              { "$group" : { "id" : 1, "_result" : { "$max" : "$modifiedDate" } } }
          ],
          "cursor" : { }
      }
      

      When running this query in mongo shell, no exception occurs.

      Can you please address this bug / issue.
      Just say that you have read it please.

      Thanks,
      Itzhak

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            itzikkg Itzhak Kagan
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: