Perform a map/reduce query using the current query spec and ordering. While ``map_reduce`` respects ``QuerySet`` chaining, it must be the last call made, as it does not return a maleable ``QuerySet``. See the :meth:`~mongoengine.tests.QuerySetTest.test_map_reduce`
(
self, map_f, reduce_f, output, finalize_f=None, limit=None, scope=None
)
| 1385 | |
| 1386 | # JS functionality |
| 1387 | def map_reduce( |
| 1388 | self, map_f, reduce_f, output, finalize_f=None, limit=None, scope=None |
| 1389 | ): |
| 1390 | """Perform a map/reduce query using the current query spec |
| 1391 | and ordering. While ``map_reduce`` respects ``QuerySet`` chaining, |
| 1392 | it must be the last call made, as it does not return a maleable |
| 1393 | ``QuerySet``. |
| 1394 | |
| 1395 | See the :meth:`~mongoengine.tests.QuerySetTest.test_map_reduce` |
| 1396 | and :meth:`~mongoengine.tests.QuerySetTest.test_map_advanced` |
| 1397 | tests in ``tests.queryset.QuerySetTest`` for usage examples. |
| 1398 | |
| 1399 | :param map_f: map function, as :class:`~bson.code.Code` or string |
| 1400 | :param reduce_f: reduce function, as |
| 1401 | :class:`~bson.code.Code` or string |
| 1402 | :param output: output collection name, if set to 'inline' will return |
| 1403 | the results inline. This can also be a dictionary containing output options |
| 1404 | see: https://www.mongodb.com/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce |
| 1405 | :param finalize_f: finalize function, an optional function that |
| 1406 | performs any post-reduction processing. |
| 1407 | :param scope: values to insert into map/reduce global scope. Optional. |
| 1408 | :param limit: number of objects from current query to provide |
| 1409 | to map/reduce method |
| 1410 | |
| 1411 | Returns an iterator yielding |
| 1412 | :class:`~mongoengine.document.MapReduceDocument`. |
| 1413 | """ |
| 1414 | queryset = self.clone() |
| 1415 | |
| 1416 | MapReduceDocument = _import_class("MapReduceDocument") |
| 1417 | |
| 1418 | map_f_scope = {} |
| 1419 | if isinstance(map_f, Code): |
| 1420 | map_f_scope = map_f.scope |
| 1421 | map_f = str(map_f) |
| 1422 | map_f = Code(queryset._sub_js_fields(map_f), map_f_scope or None) |
| 1423 | |
| 1424 | reduce_f_scope = {} |
| 1425 | if isinstance(reduce_f, Code): |
| 1426 | reduce_f_scope = reduce_f.scope |
| 1427 | reduce_f = str(reduce_f) |
| 1428 | reduce_f_code = queryset._sub_js_fields(reduce_f) |
| 1429 | reduce_f = Code(reduce_f_code, reduce_f_scope or None) |
| 1430 | |
| 1431 | mr_args = {"query": queryset._query} |
| 1432 | |
| 1433 | if finalize_f: |
| 1434 | finalize_f_scope = {} |
| 1435 | if isinstance(finalize_f, Code): |
| 1436 | finalize_f_scope = finalize_f.scope |
| 1437 | finalize_f = str(finalize_f) |
| 1438 | finalize_f_code = queryset._sub_js_fields(finalize_f) |
| 1439 | finalize_f = Code(finalize_f_code, finalize_f_scope or None) |
| 1440 | mr_args["finalize"] = finalize_f |
| 1441 | |
| 1442 | if scope: |
| 1443 | mr_args["scope"] = scope |
| 1444 |