[SERVER-32961] Add $fmod, or some way to mod by fractional numbers, to the match expression language Created: 29/Jan/18  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework, Querying
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Charlie Swanson Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: expression, mql-semantics
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
is related to DOCS-7792 $mod silently truncates decimal argum... Closed
is related to SERVER-32960 $mod has inconsistent rounding/trunca... Closed
Assigned Teams:
Query Optimization
Backwards Compatibility: Fully Compatible
Participants:

 Description   

The $mod query operator converts fractional numbers to integers to do the modular arithmetic. It still makes sense to mod by a fractional number in some scenarios.



 Comments   
Comment by David Storch [ 30/Jan/18 ]

geert.bosch, if I understand correctly, this ticket is specifically about the match expression language, not the agg expression language. I've updated the title of the ticket to clarify.

Comment by Geert Bosch [ 30/Jan/18 ]

Actually, it seems $mod aggregation operator does support fractional numbers:

> db.money.insert({amount: NumberDecimal("12.30")})
WriteResult({ "nInserted" : 1 })
> db.money.aggregate([{$addFields: { quarters: {$trunc: {$divide:["$amount", NumberDecimal("0.25")]}}, cents:{$mod: ["$amount",NumberDecimal("0.25")]}}}])
{ "_id" : ObjectId("5a7086df3130aa7be79b246f"), "amount" : NumberDecimal("12.30"), "quarters" : NumberDecimal("49"), "cents" : NumberDecimal("0.05") }

The ExpressionMod code looks correct as well.

Comment by David Storch [ 29/Jan/18 ]

As part of JSON Schema, we implemented internal support for fmod in the match expression language: $_internalSchemaFmod:

https://github.com/mongodb/mongo/blob/2487ab101a42434e1f61e68ee7c2383432861f8b/src/mongo/db/matcher/schema/expression_internal_schema_fmod.h

Assuming this implements the behavior we want in general, we could change this to be exposed to clients.

Comment by Charlie Swanson [ 29/Jan/18 ]

geert.bosch can you elaborate on the use case you were describing earlier? Not sure I followed enough to summarize myself

Generated at Thu Feb 08 04:31:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.