

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

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

バージョン 8.0.1 から新規。

Amazon DocumentDB の `$atan2`演算子は、 の逆接線 (arc 接線) を返します。ここで`y / x`、 `y` と `x`はそれぞれ式に渡される最初と 2 番目の値です。

どちらの式も数値に解決する必要があります。演算子は、両方の引数の符号を使用して正しい四分円を決定します。

結果はラジアンです。学位を取得するには、 `$radiansToDegrees`を出力に適用します。

戻り値の型は`double`デフォルトで です。入力が 128 ビットの小数の場合、出力も 128 ビットの小数です。

**パラメータ**
+ `expression 1` (y): y 座標を表す数値に解決される式。
+ `expression 2` (x): x 座標を表す数値に解決される式。

## 動作
<a name="atan2-behavior"></a>

**null と NaN**

いずれかの引数が の場合 `null` (または欠落しているフィールドを参照する場合）、結果は です`null`。いずれかの引数が の場合`NaN`、結果は です`NaN`。1 つの引数が `null`で、もう 1 つの引数が の場合`NaN`、 `null` が優先されます。


| 例 | 結果 | 
| --- | --- | 
| { $atan2: [ NaN, <value> ] } | NaN | 
| { $atan2: [ <value>, NaN ] } | NaN | 
| { $atan2: [ null, <value> ] } | null | 
| { $atan2: [ <value>, null ] } | null | 
| { $atan2: [ NaN, null ] } | null | 
| { $atan2: [ null, NaN ] } | null | 

## 例 (MongoDB シェル)
<a name="atan2-examples"></a>

次の例は、 `$atan2`演算子を使用して、ポイントのセットの y/x のアークタンジェントを計算する方法を示しています。

**サンプルドキュメントを作成する**

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

**クエリの例**

```
db.points.aggregate([
  { $project: {
    "angle": { $atan2: ["$y", "$x"] }
  }}
]);
```

**出力**

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

## コードの例
<a name="atan2-code"></a>

`$atan2` 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。

------
#### [ 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('points');

  const result = await collection.aggregate([
    { $project: {
      "angle": { $atan2: ["$y", "$x"] }
    }}
  ]).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['points']

    result = list(collection.aggregate([
        { '$project': {
            'angle': { '$atan2': ['$y', '$x'] }
        }}
    ]))

    print(result)
    client.close()

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

------