View a markdown version of this page

$tanh - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$tanh

8.0.1 版的新功能。

Amazon DocumentDB 中的$tanh運算子會傳回值的雙曲切線。

輸入表達式必須解析為數值。

傳回類型預設為 double 。如果輸入是 128 位元的小數位數,則輸出也是 128 位元的小數位數。

參數

  • expression:解析為數字的表達式。

Behavior (行為)

null、NaN 和 +/- Infinity

範例 結果
{ $tanh: NaN } NaN
{ $tanh: null } null
{ $tanh: -Infinity } -1
{ $tanh: Infinity } 1

當輸入為 null或參考欄位遺失時, $tanh會傳回 null。的輸入NaN會產生 NaN。對於正無限,結果為 1;對於負無限,結果為 -1

範例 (MongoDB Shell)

下列範例示範如何使用 $tanh運算子來計算值的雙曲切線。

建立範例文件

db.values.insertMany([ { "_id": 1, "value": 0 }, { "_id": 2, "value": 1 }, { "_id": 3, "value": -1 } ]);

查詢範例

db.values.aggregate([ { $project: { "result": { $tanh: "$value" } }} ]);

輸出

[ { "_id": 1, "result": 0 }, { "_id": 2, "result": 0.7615941559557649 }, { "_id": 3, "result": -0.7615941559557649 } ]

程式碼範例

若要檢視使用 $tanh 運算子的程式碼範例,請選擇您要使用的語言標籤:

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('values'); const result = await collection.aggregate([ { $project: { "result": { $tanh: "$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['values'] result = list(collection.aggregate([ { '$project': { 'result': { '$tanh': '$value' } }} ])) print(result) client.close() if __name__ == "__main__": main()