convert onError does not catch the date to string year range error

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Minor - P4
    • None
    • Affects Version/s: 8.2.4, 7.0.31, 8.2.6, 8.2.12
    • Component/s: None
    • None
    • Query Execution
    • ALL
    • Hide
      docker run -d --name repro mongo:8.2.12
      
      # wait for startup, then:
      docker exec repro mongosh --quiet test --eval '
        db.d.insertOne({_id: 1, ms: NumberLong("253402300800000")});   // year 10000
      
        db.d.aggregate([{$project: {_id: 0, s: {$convert: {
          input: {$toDate: "$ms"}, to: "string", onError: "caught"
        }}}}]);
      '
      
      MongoServerError: Executor error during aggregate command on namespace: test.d :: caused by :: Could not convert date to string: date component was outside the supported range of 0-9999: 10000
      
      Show
      docker run -d --name repro mongo:8.2.12 # wait for startup, then: docker exec repro mongosh --quiet test --eval ' db.d.insertOne({_id: 1, ms: NumberLong("253402300800000")}); // year 10000 db.d.aggregate([{$project: {_id: 0, s: {$convert: { input: {$toDate: "$ms"}, to: "string", onError: "caught" }}}}]); ' MongoServerError: Executor error during aggregate command on namespace: test.d :: caused by :: Could not convert date to string: date component was outside the supported range of 0-9999: 10000
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      onError is meant to replace a failed conversion with the value given instead of failing the pipeline. Converting a date outside the supported year range to a string returns error 18537 even with onError set, so the pipeline fails and the value is never used.

      onError does work for other conversion failures on the same server, so it is not being ignored outright. This returns caught as expected.

      db.d.aggregate([{$project: {_id: 0, s: {$convert: {
        input: "not a number", to: "int", onError: "caught"
      }}}}]);
      

      Dropping onError from the failing pipeline gives the same error 18537, which is correct on its own. A date inside the range converts normally, so only the onError handling is affected.

      The same thing happens with a constant instead of a field reference, though the message text differs slightly between the two.

      Expected

      A single document with s set to caught.

      Actual

      Error 18537, and no documents returned.

      The date here is built with $toDate from a millisecond value because drivers cannot represent years outside 1 to 9999.

            Assignee:
            Projjal Chanda
            Reporter:
            Daniel Frankcom (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: