

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

# \$1allElementsTrue
<a name="allElementsTrue"></a>

バージョン 4.0 の新機能

`$allElementsTrue` 演算子は、配列内のすべての要素が true 値に評価されるかどうかを確認するために使用されます。

**パラメータ**
+ `expression`: 配列に評価される式。

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

次の例は、配列内のすべての要素が true であるかどうかをチェック`$allElementsTrue`するための の使用を示しています。

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

```
db.collection.insert([
  { "name": "John", "scores": [100, 90, 80] },
  { "name": "Jane", "scores": [80, 85, 0] },
  { "name": "Bob", "scores": [90, 95, null] }
])
```

**クエリの例**

```
db.collection.find({
  "scores": { "$allElementsTrue": [{ "$gt": 0 }] }
})
```

**出力**

```
[
  { "_id" : ObjectId("..."), "name" : "John", "scores" : [ 100, 90, 80 ] },
  { "_id" : ObjectId("..."), "name" : "Bob", "scores" : [ 90, 95, null ] }
]
```

この例では、クエリは`scores`配列内のすべての要素が 0 より大きいかどうかを確認します。`scores` 配列に 0 が含まれているため、 を持つドキュメント`&quot;name&quot;: &quot;Jane&quot;`は除外されます。0 は不正な値です。

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

`$allElementsTrue` コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  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('collection');

  const result = await collection.find({
    "scores": { "$allElementsTrue": [{ "$gt": 0 }] }
  }).toArray();

  console.log(result);

  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')
    db = client['test']
    collection = db['collection']

    result = list(collection.find({
        "scores": {"$allElementsTrue": [{"$gt": 0}]}
    }))

    print(result)

    client.close()

example()
```

------