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

$match(集計ステージ)

$match

指定されたクエリ述語に基づいてドキュメントをフィルタリングします。 一致したドキュメントは、次のパイプライン ステージに渡されます。

次の環境でホストされる配置には $match を使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

{ $match: { <query predicate> } }

クエリ述語の構文は、$match コマンドの クエリ find()引数で使用される構文と同じです。

クエリ述語にを含めるには、 $expr演算子を使用します。

次のいずれかの条件に当てはまる場合、$match ステージはパイプラインの結果からドキュメントをフィルタリングで除外します。

  • $match クエリ述語は、そのドキュメントの 0null 、または false の値を返します。

  • $matchクエリ述語は、そのドキュメントには欠落しているフィールドを使用します 。

  • $matchステージでは$whereは使用できません。

  • $matchステージでは$nearまたは$nearSphereは使用できません。 あるいは、次のいずれかを実行できます。

  • To use $text in a $match stage, the $match stage has to be the first stage of the pipeline.

    ビュー$textをサポートしていません。

    注意

    $text は、自己管理型(Atlas 以外)配置に対するテキスト クエリ機能を提供します。MongoDB Atlas でホストされているデータに対して、MongoDB は改良された全文クエリ ソリューションである Atlas Search を提供します。

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 コレクション内のドキュメントをモデル化します。

[BsonIgnoreExtraElements]
public class Movie
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("title")]
public string Title { get; set; } = null!;
[BsonElement("year")]
public int? Year { get; set; }
[BsonElement("runtime")]
public int? Runtime { get; set; }
[BsonElement("rated")]
public string? Rated { get; set; }
[BsonElement("metacritic")]
public int Metacritic { get; set; }
[BsonElement("plot")]
public string? Plot { get; set; }
[BsonElement("type")]
public string? Type { get; set; }
[BsonElement("cast")]
public string[]? Cast { get; set; }
[BsonElement("directors")]
public string[]? Directors { get; set; }
[BsonElement("writers")]
public string[]? Writers { get; set; }
[BsonElement("imdb")]
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;

集計に関する詳細情報とユースケースについては、完全な集計パイプラインチュートリアルを参照してください。