ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

MongoDB\Collection::mapReduce()

Deprecated since version 1.12.

バージョン 1.2 の新機能.

MongoDB\Collection::mapReduce()

The mapReduce command allows you to run map-reduce aggregation operations over a collection.

function mapReduce(
MongoDB\BSON\JavascriptInterface $map,
MongoDB\BSON\JavascriptInterface $reduce,
string|array|object $out,
array $options = []
): MongoDB\MapReduceResult
$map : MongoDB\BSON\Javascript

A JavaScript function that associates or "maps" a value with a key and emits the key and value pair.

注意

Passing a Javascript instance with a scope is deprecated. Put all scope variables in the scope option of the MapReduce operation.

$reduce : MongoDB\BSON\Javascript

A JavaScript function that "reduces" to a single object all the values associated with a particular key.

注意

Passing a Javascript instance with a scope is deprecated. Put all scope variables in the scope option of the MapReduce operation.

$out : string|array|object
Specifies where to output the result of the map-reduce operation. You can either output to a collection or return the result inline. On a primary member of a replica set you can output either to a collection or inline, but on a secondary, only inline output is possible.
$options : 配列

必要なオプションを指定する配列。

名前
タイプ
説明

bypassDocumentValidation

ブール値

trueの場合、書込み操作によってドキュメント レベルの検証を回避できます。 デフォルトはfalseです。

This only applies when results are output to a collection.

collation

array|object

照合順序を指定すると、大文字や小文字、アクセント記号など、string を比較するための言語独自のルールを指定できます。 照合を指定する場合、 localeフィールドは必須です。その他の照合フィールドはすべてオプションです。 フィールドの説明については、 「照合ドキュメント」 を参照してください。

照合が指定されていないが、コレクションにデフォルトの照合がある場合、操作はコレクションに指定された照合を使用します。 コレクションにも操作にも照合が指定されていない場合、MongoDB では以前のバージョンで使用されていた単純なバイナリ比較によって string が比較されます。

comment

混合

データベースプロファイラcurrentOp出力、およびログから操作を追跡するのに役立つ任意のコメントを指定できるようにします。

このオプションは MongoDB 4.4 以降で使用可能であり、古いサーバー バージョンで指定すると実行時に例外が発生します。

バージョン 1.13 の新機能

finalize

Follows the reduce method and modifies the output.

Passing a Javascript instance with a scope is deprecated. Put all scope variables in the scope option of the MapReduce operation.

jsMode

ブール値

Specifies whether to convert intermediate data into BSON format between the execution of the map and reduce functions.

limit

integer

Specifies a maximum number of documents for the input into the map function.

maxTimeMS

integer

カーソルに対する情報処理操作の累積時間制限(ミリ秒単位)。 MongoDB は、次の割り込みポイントが最も近い時点で操作を中止します。

クエリ

array|object

Specifies the selection criteria using query operators for determining the documents input to the map function.

ReadConcern

操作に使用する読み取り保証。 デフォルトはコレクションの読み取り保証 (read concern) です。

トランザクションの一部である個々の操作に対して読み取り保証を指定することはできません。 代わりに 、トランザクションを開始する ときにreadConcern オプションを設定します。

readPreference

操作に使用する読み取り設定。 コレクションの読み込み設定(read preference)がデフォルトで設定されます。

This option will be ignored when results are output to a collection.

scope

array|object

Specifies global variables that are accessible in the map, reduce, and finalize functions.

セッション

操作に関連付けるクライアント セッション。

バージョン 1.3 で追加

sort

array|object

結果の順序の並び替え指定。

typeMap

配列

型マップ カーソルに適用される 。これは、BSON ドキュメントが PHP 値に変換される方法を決定します。コレクションのタイプ マップがデフォルトになります。

verbose

ブール値

Specifies whether to include the timing information in the result information.

writeConcern

操作に使用する書込み保証 ( write concern )。 コレクションの書込み保証 (write concern) のデフォルトです。

トランザクションの一部である個々の操作に対して書込み保証 (write concern) を指定することはできません。 代わりに 、トランザクションを開始する ときにwriteConcern オプションを設定します。

A MongoDB\MapReduceResult object, which allows for iteration of map-reduce results irrespective of the output method (e.g. inline, collection) via the IteratorAggregate interface. It also provides access to command statistics.

MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collationreadConcernwriteConcern )。

パラメータまたはオプションの解析に関連するエラーは、 MongoDB\Exception\InvalidArgumentException

サーバーからのコマンド応答が不正な形式であった場合は、 MongoDB\Exception\UnexpectedValueException

MongoDB\Driver\Exception\RuntimeException 拡張レベルのその他のエラー(例:)。

In MongoDB, the map-reduce operation can write results to a collection or return the results inline. If you write map-reduce output to a collection, you can perform subsequent map-reduce operations on the same input collection that merge replace, merge, or reduce new results with previous results. See Map-Reduce and Perform Incremental Map-Reduce for details and examples.

When returning the results of a map-reduce operation inline, the result documents must be within the BSON Document Size limit, which is currently 16 megabytes.

MongoDB supports map-reduce operations on sharded collections. Map-reduce operations can also output the results to a sharded collection. See Map-Reduce and Sharded Collections.

This example will use city populations to calculate the overall population of each state.

<?php
$collection = (new MongoDB\Client)->test->zips;
$map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }');
$reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }');
$out = ['inline' => 1];
$populations = $collection->mapReduce($map, $reduce, $out);
foreach ($populations as $pop) {
var_dump($pop);
};

出力は次のようになります。

object(stdClass)#2293 (2) {
["_id"]=>
string(2) "AK"
["value"]=>
float(544698)
}
object(stdClass)#2300 (2) {
["_id"]=>
string(2) "AL"
["value"]=>
float(4040587)
}
object(stdClass)#2293 (2) {
["_id"]=>
string(2) "AR"
["value"]=>
float(2350725)
}
object(stdClass)#2300 (2) {
["_id"]=>
string(2) "AZ"
["value"]=>
float(3665228)
}
  • mapReduce command reference in the MongoDB manual

  • Map-Reduce documentation in the MongoDB manual