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

$or(式演算子)

$or

Evaluates one or more expressions and returns true if any of the expressions are true. Otherwise, $or returns false.

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

{ $or: [ <expression1>, <expression2>, ... ] }

式の詳細については、「式 」を参照してください。

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

例
結果

{ $or: [ true, false ] }

true

{ $or: [ [ false ], false ] }

true

{ $or: [ null, 0, undefined ] }

false

{ $or: [ ] }

false

クエリ エンジンでクエリが最適化されるよう、$or はエラーを次のように処理します。

  • $or に指定した式が単独で評価されるとエラーが発生する場合、その式を含む $or でエラーが発生することがありますが、必ずしもエラーが発生するわけではありません。

  • $or に最初に指定した式の後に式を指定すると、最初の式が true と評価されていてもエラーが発生することがあります。

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: "abc1", description: "product 1", qty: 300 },
{ _id: 2, item: "abc2", description: "product 2", qty: 200 },
{ _id: 3, item: "xyz1", description: "product 3", qty: 250 },
{ _id: 4, item: "VWZ1", description: "product 4", qty: 300 },
{ _id: 5, item: "VWZ2", description: "product 5", qty: 180 }
] )

The following operation uses the $or operator to determine if qty is greater than 250 or less than 200:

db.inventory.aggregate(
[
{
$project:
{
item: 1,
result: { $or: [ { $gt: [ "$qty", 250 ] }, { $lt: [ "$qty", 200 ] } ] }
}
}
]
)

この操作は次の結果を返します。

{ _id: 1, item: "abc1", result: true }
{ _id: 2, item: "abc2", result: false }
{ _id: 3, item: "xyz1", result: false }
{ _id: 4, item: "VWZ1", result: true }
{ _id: 5, item: "VWZ2", result: true }
このページを評価