$abs - Amazon DocumentDB

$abs

The $abs operator in Amazon DocumentDB returns the absolute value of a number. It can be used in the aggregation pipeline to perform mathematical operations on numeric fields.

Parameters

  • number: The numeric expression for which the absolute value will be returned.

Example (MongoDB Shell)

This example demonstrates the usage of the $abs operator to find the absolute value of a numeric field.

Create sample documents

db.numbers.insertMany([ { "_id": 1, "value": -5 }, { "_id": 2, "value": 10 }, { "_id": 3, "value": -3.14 }, { "_id": 4, "value": 0 } ]);

Query example

db.numbers.aggregate([ { $project: { "_id": 1, "absolute_value": { $abs: "$value" } }} ]);

Output

[ { "_id": 1, "absolute_value": 5 }, { "_id": 2, "absolute_value": 10 }, { "_id": 3, "absolute_value": 3.14 }, { "_id": 4, "absolute_value": 0 } ]

Code examples

To view a code example for using the $abs command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('numbers'); const result = await collection.aggregate([ { $project: { "_id": 1, "absolute_value": { $abs: "$value" } }} ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient def main(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['numbers'] result = list(collection.aggregate([ { '$project': { "_id": 1, "absolute_value": { "$abs": "$value" } }} ])) print(result) client.close() if __name__ == "__main__": main()