View a markdown version of this page

$trunc - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$trunc

New from version 8.0.1.

The $trunc operator in Amazon DocumentDB truncates a number to a specified decimal place, removing digits without rounding.

Parameters

  • number: An expression that resolves to a number.

  • place: Optional. An integer expression between -20 and 100 that specifies the number of decimal places to truncate to. A negative value truncates to the left of the decimal. Defaults to 0.

Example (MongoDB Shell)

The following example shows how to use the $trunc operator to truncate numeric values to one decimal place.

Truncating with a positive place value

db.measurements.insertMany([ {_id: 1, value: 3.456}, {_id: 2, value: 7.891}, {_id: 3, value: 12.345} ]);

Query example

db.measurements.aggregate([ { $project: { truncated: { $trunc: ["$value", 1] } } } ]);

Output

[ {_id: 1, truncated: 3.4}, {_id: 2, truncated: 7.8}, {_id: 3, truncated: 12.3} ]

Truncating with a negative place value

When place is negative, $trunc replaces digits to the left of the decimal with zeros. The following example truncates values using the first digit to the left of the decimal.

db.samples.insertMany([ {_id: 1, value: 19.25}, {_id: 2, value: 28.73}, {_id: 3, value: 34.32} ]);
db.samples.aggregate([ { $project: { truncated: { $trunc: ["$value", -1] } } } ]);

Output

[ {_id: 1, truncated: 10}, {_id: 2, truncated: 20}, {_id: 3, truncated: 30} ]

Code examples

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

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