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