定義
$match指定されたクエリ述語に基づいてドキュメントをフィルタリングします。 一致したドキュメントは、次のパイプライン ステージに渡されます。
互換性
次の環境でホストされる配置には $match を使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
{ $match: { <query predicate> } }
動作
パイプラインの最適化
If you place a
$matchat the very beginning of a pipeline, the query can take advantage of indexes like any otherdb.collection.find()ordb.collection.findOne().
クエリ述語の式
0 、NULL、false、または欠損値
次のいずれかの条件に当てはまる場合、$match ステージはパイプラインの結果からドキュメントをフィルタリングで除外します。
$matchクエリ述語は、そのドキュメントの0、null、またはfalseの値を返します。$matchクエリ述語は、そのドキュメントには欠落しているフィールドを使用します 。
制限事項
$matchステージでは$whereは使用できません。$matchステージでは$nearまたは$nearSphereは使用できません。 あるいは、次のいずれかを実行できます。Use the
$geoWithinquery predicate operator with$centeror$centerSpherein the$matchstage.
To use
$textin a$matchstage, the$matchstage has to be the first stage of the pipeline.ビューは
$textをサポートしていません。注意
$textは、自己管理型(Atlas 以外)配置に対するテキスト クエリ機能を提供します。MongoDB Atlas でホストされているデータに対して、MongoDB は改良された全文クエリ ソリューションである Atlas Search を提供します。
Atlas Search を使用した Atlas のデータのフィルタリング
For data stored in MongoDB Atlas, you can use the Atlas Search compound Operator operator filter option to match or filter documents when running $search queries. Running $match after $search is less performant than running $search with the compound Operator operator filter option.
filter オプションの詳細については、Atlas ドキュメントの「複合演算子」を参照してください。
例
このページの例では、sample_mflixサンプルデータセットのデータを使用します。このデータセットを自己管理型MongoDB配置にロードする方法の詳細については、サンプルデータセットをロードする を参照してください。サンプルデータベースに変更を加えた場合、このページの例を実行するには、データベースを削除して再作成する必要がある場合があります。
等価一致
The following operation uses $match to perform an equality match on the rated field. The runtime filter limits the result to a small, representative set:
db.movies.aggregate( [ { $match : { rated : "TV-PG", runtime : { $gt: 1000 } } } ] )
The $match selects the documents where the rated field equals "TV-PG" and runtime is greater than 1000.
カウントの実行
The following example selects documents to process using the $match pipeline operator and then pipes the results to the $group pipeline operator to compute a count of the documents:
db.movies.aggregate( [ { $match: { $or: [ { runtime: { $gt: 1000 } }, { year: { $lt: 1910 } } ] } }, { $group: { _id: null, count: { $sum: 1 } } } ] )
[ { _id: null, count: 6 } ]
In the aggregation pipeline, $match selects the documents where either the runtime is greater than 1000 or the year is earlier than 1910. These documents are then sent to the $group to perform a count.
配列要素の一致
配列フィールド内の要素に基づいてdocumentをフィルタリングするには、$match ステージのクエリ述語で $elemMatch 演算子を使用します。
db.aggregate( [ { $documents: [ { student_id: 1, scores: [ 0.75, 0.65, 0.73 ] }, { student_id: 2, scores: [ 0.9, 0.88, 0.98 ] }, { student_id: 3, scores: [ 0.9, 0.84, 0.93 ] } ] }, { $match: { scores: { $elemMatch: { $gte: 0.9 } } } } ] )
[ { student_id: 2, scores: [ 0.9, 0.88, 0.98 ] }, { student_id: 3, scores: [ 0.9, 0.84, 0.93 ] } ]
このページのC#の例では、Atlasサンプルデータセット の sample_mflixデータベースを使用します。MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 MongoDB .NET/ C#ドライバーのドキュメントの「 開始 」を参照してください。
次の Movie クラスは、sample_mflix.movies コレクション内のドキュメントをモデル化します。
[] public class Movie { [] public ObjectId Id { get; set; } [] public string Title { get; set; } = null!; [] public int? Year { get; set; } [] public int? Runtime { get; set; } [] public string? Rated { get; set; } [] public int Metacritic { get; set; } [] public string? Plot { get; set; } [] public string? Type { get; set; } [] public string[]? Cast { get; set; } [] public string[]? Directors { get; set; } [] public string[]? Writers { get; set; } [] public ImdbData? Imdb { get; set; } }
To use the MongoDB .NET/C# driver to add a $match stage to an aggregation pipeline, call the UnionWith() method on a PipelineDefinition object.
次の例では、Titleフィールドが "The Godfather" に等しいすべての Movie ドキュメントに一致するパイプラインステージを作成します。
var pipeline = new EmptyPipelineDefinition<Movie>() .Match(m => m.Title == "The Godfather");
{ "_id" : "...", "title" : "The Godfather", "metacritic" : 100, "rated" : "R", "runtime" : 175, "imdb" : "..." }
このページのNode.js の例では、Atlasサンプルデータセット の sample_mflixデータベースを使用します。無料のMongoDB Atlas cluster を作成し、サンプルデータセットをロードする方法については、 MongoDB Node.jsドライバーのドキュメントの開始を参照してください。
MongoDB Node.jsドライバーを使用して $match ステージを集計パイプラインに追加するには、パイプラインオブジェクトで $match 演算子を使用します。
次の例では、title フィールドが "The Shawshank Redemption" に等しいすべての movie ドキュメントに一致するパイプラインステージを作成します。次に、この例は集計パイプラインを実行します。
const pipeline = [ { $match: { title: "The Shawshank Redemption" } } ]; const cursor = collection.aggregate(pipeline); return cursor;
詳細
集計に関する詳細情報とユースケースについては、完全な集計パイプラインチュートリアルを参照してください。