Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-5400

[Server] Explain how $mod handles negative numbers

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • Server_Docs_[20240312]
    • Affects Version/s: None
    • Component/s: manual, Server
    • Labels:
    • Environment:

      The $mod function doesn't describe how it behaves for negative numbers.

      What's -1 % 10, for example? Programming languages are generally split on how to answer this question. Python and Ruby say 9, while Java and C say -1. A nonnegative result is arguably more (mathematically) correct, but MongoDB generally follows Javascript, so maybe it gives -1 here?

      Whichever one MongoDB does today, I can figure out by trial-and-error, but since it's not documented, is it guaranteed to be stable?

       

      [AB: Seems like it would be a good idea to update the query operator $mod page also.]

      Please see the $mod (eval query operator) vs $mod (aggregation) Behavior Report for background information, testing information, and pertinent questions.

            Assignee:
            joseph.dougherty@mongodb.com Joseph Dougherty
            Reporter:
            xgen-internal-docs Docs Collector User (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:
              1 year, 26 weeks, 6 days ago