AggregateCommand.abs(value: Expression<number>): Object

View Markdown

Last edited on 2026-07-04

Supported on: Mini Program 2.7.4, Cloud Function 0.8.1, Web

Aggregation operator. Returns the absolute value of a number.

Parameters

value: Expression<number>

number

Return Value

Object

API Description

The syntax is as follows:

db.command.aggregate.abs(<number>)

The value passed to abs can be a numeric constant or any expression that ultimately resolves to a number.

If the expression resolves to null or refers to a non-existent field, the result of abs is null. If the value resolves to NaN, the result is NaN.

Example Code

Suppose the collection ratings contains the following records:

{ _id: 1, start: 5, end: 8 }
{ _id: 2, start: 4, end: 4 }
{ _id: 3, start: 9, end: 7 }
{ _id: 4, start: 6, end: 7 }

··· You can calculate the absolute difference between start and end for each record as follows:

const $ = db.command.aggregate
db.collection('ratings').aggregate()
  .project({
    delta: $.abs($.subtract(['$start', '$end']))
  })
  .end()

The returned results are as follows:

{ "_id" : 1, "delta" : 3 }
{ "_id" : 2, "delta" : 0 }
{ "_id" : 3, "delta" : 2 }
{ "_id" : 4, "delta" : 1 }