[SERVER-10176] Implement $abs (absolute value) operator for aggregation framework Created: 11/Jul/13  Updated: 05/Feb/16  Resolved: 10/Jun/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 2.4.5
Fix Version/s: 3.1.5

Type: Improvement Priority: Minor - P4
Reporter: Gary Rechnitz Assignee: James Cohan
Resolution: Done Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-1372 Implement $abs (absolute value) opera... Closed
is related to DRIVERS-234 Aggregation Builder Support for 3.2 Closed
Backwards Compatibility: Fully Compatible
Sprint: Quint Iteration 5
Participants:

 Description   

Include $abs (absolute value) arithmetic operator for use in $project and $group



 Comments   
Comment by Charlie Swanson [ 11/Jun/15 ]

This was committed as of https://github.com/mongodb/mongo/commit/cfbd477d732bdc9855bffc5434e3a4eb1ce8e588

Comment by James Cohan [ 10/Jun/15 ]

Need to document new $abs expression.

$abs takes a numeric argument and makes it positive. It will error on long long min and it will convert min int into a long. In all other cases it preserves type. Null values remain null. It will error on all nonnumeric and non-null types.

Comment by nifan [ 01/Jun/15 ]

Status is " In Code Review " does this mean there is already a patch for it somewhere ?

Comment by guipulsar [ 01/Jun/15 ]

really need it... can't wait anymore

Comment by nifan [ 01/Jun/15 ]

While the $cond solution works it is more like a workaround then a solution.

Is there any reason in MongoDB why these kind's of basic operators would be hard to implement ?

Comment by Kevin Kelm [ 13/Aug/13 ]

Can that possibly have even a fraction of the performance of an actual native implementation of abs() ?

Comment by Gary Rechnitz [ 13/Aug/13 ]

Yes, you're of course correct, but this is a fairly fundamental operation. I'm happy with queries in the shell, but this is the kind of thing that SQL people complain about being unnecessarily difficult. They'd joke that perhaps they'd probably have to use iterative subtraction instead of division. I'm trying to engage these engineers in the ease and elegance of the aggregation framework instead of push them away. But thanks for the consideration.

Comment by Stennie Steneker (Inactive) [ 13/Aug/13 ]

Hi Gary,

Thank you for the feature suggestion.

FYI, it is already possible to calculate an absolute value using the conditional operator ($cond):

db.example.insert({'somevalue': -42});
db.example.insert({'somevalue': 0})
db.example.insert({'somevalue': 21})
 
db.example.aggregate(
	{ $project: {
		_id : 0,
		'absvalue' : { $cond: [ {$lte: ['$somevalue',0]}, { $multiply: ['$somevalue', -1] }, '$somevalue'] }
	}}
)
{
	"result" : [
		{
			"absvalue" : 42
		},
		{
			"absvalue" : 0
		},
		{
			"absvalue" : 21
		}
	],
	"ok" : 1
}

Regards,
Stephen

Generated at Thu Feb 08 03:22:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.