このバージョンのドキュメントはアーカイブされており、サポートされなくなりました。5.0 配置をアップグレードするには、 MongoDB 6.0 のアップグレード手順 を参照してください。
定義
- $anyElementTrue
- 配列をセットとして評価し、いずれかの要素が - trueと- falseである場合は- trueを返します。 空の配列は- falseを返します。- $anyElementTrueの構文は次のとおりです。- { $anyElementTrue: [ <expression> ] } - <expression>自体は、引数リストを示す外側の配列とは別に、配列に解決される必要があります。 式の詳細については、「 式 」を参照してください。
動作
セットにネストされた配列要素が含まれている場合、 $anyElementTrueはネストされた配列に下降 せず、最上位の配列を評価します。
falseブール値に加えて、 $anyElementTrueは、次のnull 、 0 、 undefined値をfalseとして評価します。 $anyElementTrueは、ゼロ以外の数値と配列を含む他のすべての値をtrueとして評価します。
| 例 | 結果 | 
|---|---|
| 
 | 
 | 
| 
 | 
 | 
| 
 | 
 | 
| 
 | 
 | 
例
次のドキュメントを使用して、 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 ] } ]) 
次の操作では、 $anyElementTrue演算子を使用して、 responses配列にtrueと評価される値が含まれているかどうかを判断します。
db.survey.aggregate(    [      { $project: { responses: 1, isAnyTrue: { $anyElementTrue: [ "$responses" ] }, _id: 0 } }    ] ) 
この操作は次の結果を返します。
{ "responses" : [ true ], "isAnyTrue" : true } { "responses" : [ true, false ], "isAnyTrue" : true } { "responses" : [ ], "isAnyTrue" : false } { "responses" : [ 1, true, "seven" ], "isAnyTrue" : true } { "responses" : [ 0 ], "isAnyTrue" : false } { "responses" : [ [ ] ], "isAnyTrue" : true } { "responses" : [ [ 0 ] ], "isAnyTrue" : true } { "responses" : [ [ false ] ], "isAnyTrue" : true } { "responses" : [ null ], "isAnyTrue" : false } { "responses" : [ undefined ], "isAnyTrue" : false }