MongoDB 条件演算子
この章では、MongoDB で条件演算子を使用する方法について説明します。
MongoDB では、条件演算子はドキュメントをクエリする際のフィルタリング、比較、論理演算に使用されます。
これらの演算子は、比較演算子、論理演算子、要素演算子、配列演算子、およびその他の一般的な演算子に分類できます。
比較演算子
比較演算子には次のものがあります:
| 演算子 | 説明 | 例 |
|---|---|---|
$eq | 等しい | { age: { $eq: 25 } } |
$ne | 等しくない | { age: { $ne: 25 } } |
$gt | より大きい | { age: { $gt: 25 } } |
$gte | 以上 | { age: { $gte: 25 } } |
$lt | より小さい | { age: { $lt: 25 } } |
$lte | 以下 | { age: { $lte: 25 } } |
$in | 指定された配列内にある | { age: { $in: [25, 30, 35] } } |
$nin | 指定された配列内にない | { age: { $nin: [25, 30, 35] } } |
$eq (等しい)
構文形式:
{ field: { $eq: value } }
年齢が25に等しい人を検索:
db.collection.find({ age: { $eq: 25 } })
$ne (等しくない)
構文形式:
{ field: { $ne: value } }
年齢が25に等しくない人を検索:
db.collection.find({ age: { $ne: 25 } })
$gt (より大きい)
構文形式:
{ field: { $gt: value } }
年齢が25より大きい人を検索:
db.collection.find({ age: { $gt: 25 } })
$gte (以上)
構文形式:
{ field: { $gte: value } }
年齢が25以上の人を検索:
db.collection.find({ age: { $gte: 25 } })
$lt (より小さい)
構文形式:{ field: { $lt: value } }年齢が25より小さい人を検索:
db.collection.find({ age: { $lt: 25 } })
$lte (以下)
構文形式:
{ field: { $lte: value } }
年齢が25以下の人を検索:
db.collection.find({ age: { $lte: 25 } })
$in (値が配列内にある)
構文形式:
{ field: { $in: [value1, value2, ...] } }
年齢が20、25、30歳の人を検索:
db.collection.find({ age: { $in: [20, 25, 30] } })
$nin (値が配列内にない)
構文形式:
{ field: { $nin: [value1, value2, ...] } }
年齢が20、25、30歳ではない人を検索:
db.collection.find({ age: { $nin: [20, 25, 30] } })
論理演算子
論理演算子には次のものがあります:
| 演算子 | 説明 | 例 |
|---|---|---|
$and | 論理積、すべての条件を満たす | { $and: [ { age: { $gt: 25 } }, { city: "New York" } ] } |
$or | 論理和、いずれかの条件を満たす | { $or: [ { age: { $lt: 25 } }, { city: "New York" } ] } |
$not | 否定、条件を満たさない | { age: { $not: { $gt: 25 } } } |
$nor | 論理否定(NOR)、すべての条件を満たさない | { $nor: [ { age: { $gt: 25 } }, { city: "New York" } ] } |
$and (すべての条件が成立)
構文形式:
{ $and: [ { condition1 }, { condition2 } ] }
年齢が25より大きく35より小さい人を検索:
db.collection.find({ $and: [{ age: { $gt: 25 } }, { age: { $lt: 35 } }] })
$or (少なくとも1つの条件が成立)
構文形式:
{ $or: [ { condition1 }, { condition2 } ] }
年齢が25より大きい、または18より小さい人を検索:
db.collection.find({ $or: [{ age: { $gt: 25 } }, { age: { $lt: 18 } }] })
$not (条件を否定)
構文形式:
{ field: { $not: { condition } } }
年齢が25より大きくない人を検索:
db.collection.find({ age: { $not: { $gt: 25 } } })
$nor (いずれの条件も成立しない)
構文形式:
{ $nor: [ { condition1 }, { condition2 } ] }
年齢が25より大きくなく、かつ18より小さくない人を検索:
db.collection.find({ $nor: [{ age: { $gt: 25 } }, { age: { $lt: 18 } }] })
要素演算子
要素演算子には次のものがあります:
| 演算子 | 説明 | 例 |
|---|---|---|
$exists | フィールドが存在するかどうか | { age: { $exists: true } } |
$type | フィールドのBSON型 | { age: { $type: "int" } } |
$exists (フィールドが存在するか)
構文形式:
{ field: { $exists: boolean } }
ageフィールドを含むドキュメントを検索:
db.collection.find({ age: { $exists: true } })
$type (フィールドの型)
構文形式:
{ field: { $type: "type" } }
ageフィールドが整数型のドキュメントを検索:
db.collection.find({ age: { $type: "int" } })
配列演算子
配列演算子には次のものがあります:
| 演算子 | 説明 | 例 |
|---|---|---|
$all | 配列が指定されたすべての要素を含む | { tags: { $all: ["red", "blue"] } } |
$elemMatch | 配列内の要素が指定された条件に一致する | { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } } |
$size | 配列の長さが指定された値と等しい | { tags: { $size: 3 } } |
配列tagsに"red"と"blue"を含むドキュメントを検索:
db.myCollection.find({ tags: { $all: ["red", "blue"] } });
$all (配列が指定されたすべての要素を含む)
構文形式:
{ field: { $all: [value1, value2, ...] } }tags配列に"mongodb"と"database"を含むドキュメントを検索:
db.collection.find({ tags: { $all: ["mongodb", "database"] } })
$elemMatch (配列内の少なくとも1つの要素が条件に一致する)
構文形式:{ field: { $elemMatch: { condition } } }
items配列内のquantityが10より大きい要素を検索:
db.collection.find({ items: { $elemMatch: { quantity: { $gt: 10 } } } })
$size (配列のサイズ)
構文形式:
{ field: { $size: value } }
tags配列のサイズが3のドキュメントを検索:
db.collection.find({ tags: { $size: 3 } })
その他の演算子
その他の演算子には以下のものがあります:
| 演算子 | 説明 | 例 |
|---|---|---|
$regex | 正規表現に一致 | { name: { $regex: /^A/ } } |
$text | テキスト検索を実行 | { $text: { $search: "coffee" } } |
$where | JavaScript式を使用して条件フィルタリングを実行 | { $where: "this.age > 25" } |
| $near | 指定したポイントに近いドキュメントを検索 | db.collection.find({ location: { $near: [10, 20], $maxDistance: 1000 } }) |
| $geoWithin | 指定した地理領域内のドキュメントを検索 | db.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } }) |
$regex (正規表現マッチング)
構文形式:
{ field: { $regex: "pattern" } }nameが"John"で始まるドキュメントを検索:
db.collection.find({ name: { $regex: /^John/ } })$near (指定したポイントに近いドキュメント)
構文形式:
{ field: { $near: [longitude, latitude], $maxDistance: distance } }
特定の位置に近いドキュメントを検索:
db.collection.find({ location: { $near: [10, 20], $maxDistance: 1000 } })
$geoWithin (指定した領域内のドキュメントを検索)
構文形式:
{ field: { $geoWithin: { $centerSphere: [[longitude, latitude], radius] } } }
指定した地理領域内のドキュメントを検索:
db.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } }) その他の拡張