Map
mapReduce returns one { key, value } document per match. It writes nothing.
mapReduce reads one collection and returns a cursor of pairs. The read stays on the process that received the command. On a replica set every voter already stores every document, so the command does not fan out. On a shard router it visits every shard and the batches are concatenated.
The command
db.runCommand({
mapReduce: "emp",
query: { deptno: 20 },
map: { key: "$job", value: "$sal" }
})
query is a find filter. Leave it out and every document matches. map is a document with key and value. A string that starts with $ reads that field, including a dotted path. Any other value is a literal. A missing field becomes null.
The reply is firstBatch, the same cursor shape as find. Each document is { key, value }. For the employees in department 20, the pairs are the job and the salary of each person.
What is refused
- A JavaScript function in
map. The error names it. Pass the{ key, value }document. reduce. The error names the field. Grouping belongs to aggregate.out. The error names the field. Nothing is written, andlistCollectionsstill shows only the source.- A command that carries
txnId. Map runs outside a transaction.
A shell helper that always sends out will get that refusal. Send runCommand as written above.