Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-98295

New $error aggregation operator to terminate a query with a custom error message

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution

      It would be useful to have a way to terminate a query with a custom error message. This would be particularly useful in the LINQ translator for the C# driver.

      For example, LINQ has a method called `Single` that verifies that an array has a single element and returns that element. The query should terminate if the array is empty or has more than one element.

      Assumingn the existence a `$error` operator to terminate the query, the following LINQ expression:

      x.ArrayField.Single()
      

      could be translated to something like the following MQL:

      { $switch :
          branches : [
              { case : { $eq : [{ $size : "$ArrayFIeld" }, 0] }, then : { $error : "array is empty" } },
              { case : { $gt : [{ $size : "$ArrayFIeld" }, 1] }, then : { $error : "array has more than one element" } }
          ],
          default; { $arrayElemAt : ["$ArrayField", 0] }
      }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: