

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

# $atan
<a name="atan"></a>

8.0.1 版的新功能。

Amazon DocumentDB 中的`$atan`運算子會傳回值的反正切 (arc 正切）。

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

結果以弧度表示。若要取得度數，請`$radiansToDegrees`套用至輸出。

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

**參數**
+ `expression`：解析為數字的表達式。

## Behavior (行為)
<a name="atan-behavior"></a>

**Null 和 NaN**


| 範例 | 結果 | 
| --- | --- | 
| { $atan: NaN } | NaN | 
| { $atan: null } | null | 

當輸入為 `null`或參考欄位遺失時，結果為 `null`。的輸入`NaN`會產生 `NaN`。

## 範例 (MongoDB Shell)
<a name="atan-examples"></a>

下列範例示範如何使用 `$atan`運算子來計算值的正切。

**建立範例文件**

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

**查詢範例**

```
db.values.aggregate([
  { $project: {
    "angle": { $atan: "$value" }
  }}
]);
```

**輸出**

```
[
  { "_id": 1, "angle": 0 },
  { "_id": 2, "angle": 0.7853981633974483 },
  { "_id": 3, "angle": -0.7853981633974483 }
]
```

## 程式碼範例
<a name="atan-code"></a>

若要檢視使用 `$atan` 運算子的程式碼範例，請選擇您要使用的語言標籤：

------
#### [ 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: {
      "angle": { $atan: "$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': {
            'angle': { '$atan': '$value' }
        }}
    ]))

    print(result)
    client.close()

if __name__ == "__main__":
    main()
```

------