AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$anyElementTrue(式)

$anyElementTrue

配列をセットとして評価し、いずれかの要素がtrueとfalseである場合はtrueを返します。 空の配列はfalseを返します。

$anyElementTrue の構文は次のとおりです。

{ $anyElementTrue: [ <expression> ] }

<expression>自体は、引数リストを示す外側の配列とは別に、配列に解決される必要があります。 式の詳細については、「式 」を参照してください。

If a set contains a nested array element, $anyElementTrue does not descend into the nested array but evaluates the array at top-level.

In addition to the false boolean value, $anyElementTrue evaluates as false the following: null, 0, and undefined values. The $anyElementTrue evaluates all other values as true, including non-zero numeric values and arrays.

例
結果

{ $anyElementTrue: [ [ true, false ] ] }

true

{ $anyElementTrue: [ [ [ false ] ] ] }

true

{ $anyElementTrue: [ [ null, false, 0 ] ] }

false

{ $anyElementTrue: [ [ ] ] }

false

次のドキュメントを使用して、 surveyという名前のサンプル コレクションを作成します。

db.survey.insertMany( [
{ _id: 1, responses: [ true ] },
{ _id: 2, responses: [ true, false ] },
{ _id: 3, responses: [ ] },
{ _id: 4, responses: [ 1, true, "seven" ] },
{ _id: 5, responses: [ 0 ] },
{ _id: 6, responses: [ [ ] ] },
{ _id: 7, responses: [ [ 0 ] ] },
{ _id: 8, responses: [ [ false ] ] },
{ _id: 9, responses: [ null ] },
{ _id: 10, responses: [ undefined ] }
] )

The following operation uses the $anyElementTrue operator to determine if the responses array contains any value that evaluates to true:

db.survey.aggregate(
[
{ $project: { responses: 1, isAnyTrue: { $anyElementTrue: [ "$responses" ] }, _id: 1 } }
]
)
[
{ _id: 1, responses: [ true ], isAnyTrue: true },
{ _id: 2, responses: [ true, false ], isAnyTrue: true },
{ _id: 3, responses: [], isAnyTrue: false },
{ _id: 4, responses: [ 1, true, 'seven' ], isAnyTrue: true },
{ _id: 5, responses: [ 0 ], isAnyTrue: false },
{ _id: 6, responses: [ [] ], isAnyTrue: true },
{ _id: 7, responses: [ [ 0 ] ], isAnyTrue: true },
{ _id: 8, responses: [ [ false ] ], isAnyTrue: true },
{ _id: 9, responses: [ null ], isAnyTrue: false },
{ _id: 10, responses: [ null ], isAnyTrue: false }
]

結果より:

  • _id: 1を含むドキュメントはtrueです。これは、 responses配列内の要素がtrueと評価されるためです。

  • と_id: 2 _id: 4trueを含むドキュメントは 配列内の少なくとも 1 つの要素がresponses trueとして評価されているため、 です。

  • _id: 6 、 _id: 7 、 _id: 8を持つドキュメントがtrueであるのは、 $anyElementTrueが操作を評価した配列であるresponses配列にネストされた配列が含まれているためで、 $anyElementTrueは常に次のように評価しますtrue 。

このページを評価

項目一覧