Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$allElementsTrue(集計)

項目一覧

  • 定義
  • 動作
$allElementsTrue

配列をセットとして評価し、配列内のどの要素もfalseでない場合は、 trueを返します。 それ以外の場合、 はfalseを返します。 空の配列はtrueを返します。

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

{ $allElementsTrue: [ <expression> ] }

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

セットにネストされた配列要素が含まれている場合、 $allElementsTrueはネストされた配列に下降 せ、最上位の配列を評価します。

falseブール値に加えて、 $allElementsTrueは、次のnull0undefined値をfalseとして評価します。 $allElementsTrueは、ゼロ以外の数値と配列を含む他のすべての値をtrueとして評価します。

結果
{ $allElementsTrue: [ [ true, 1, "someString" ] ] }
true
{ $allElementsTrue: [ [ [ false ] ] ] }
true
{ $allElementsTrue: [ [ ] ] }
true
{ $allElementsTrue: [ [ null, false, 0 ] ] }
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 ] }
])

次の操作では、 $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 }

戻る

$addToSet

項目一覧