@@ -304,7 +304,7 @@ public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, bo
304
304
cmd .append ( "fields" , fields );
305
305
if (sort != null && !sort .keySet ().isEmpty ())
306
306
cmd .append ( "sort" , sort );
307
-
307
+
308
308
if (remove )
309
309
cmd .append ( "remove" , remove );
310
310
else {
@@ -904,47 +904,94 @@ public List distinct( String key , DBObject query ){
904
904
905
905
/**
906
906
* performs a map reduce operation
907
- * @param map map function in javascript code
908
- * @param outputTarget optional - leave null if want to use temp collection
909
- * @param reduce reduce function in javascript code
910
- * @param query to match
911
- * @return
907
+ * Runs the command in INLINE output mode
908
+ *
909
+ * @param map
910
+ * map function in javascript code
911
+ * @param reduce
912
+ * reduce function in javascript code
913
+ * @param query
914
+ * to match
915
+ * @return
912
916
* @throws MongoException
913
917
* @dochub mapreduce
914
918
*/
915
- public MapReduceOutput mapReduce ( String map , String reduce , Object outputTarget , DBObject query )
916
- throws MongoException {
917
- BasicDBObjectBuilder b = BasicDBObjectBuilder .start ()
918
- .add ( "mapreduce" , _name )
919
- .add ( "map" , map )
920
- .add ( "reduce" , reduce );
921
-
922
- if ( outputTarget == null ){
923
- }
924
- else if ( outputTarget instanceof String ){
925
- b .add ( "out" , outputTarget );
926
- }
927
- else if ( outputTarget instanceof org .bson .BSONObject ){
928
- b .add ( "out" , outputTarget );
929
- }
930
- else {
931
- throw new IllegalArgumentException ( "outputTarget has to be a string or a document" );
932
- }
919
+ public MapReduceOutput mapReduce ( String map , String reduce , DBObject query ) throws MongoException {
920
+ return mapReduce ( new MapReduceCommand ( getName () , map , reduce , query ) );
921
+ }
922
+
923
+ /**
924
+ * performs a map reduce operation
925
+ * Runs the command in STANDARD output mode (saves to named collection)
926
+ *
927
+ * @param map
928
+ * map function in javascript code
929
+ * @param outputTarget
930
+ * optional - leave null if want to use temp collection
931
+ * @param reduce
932
+ * reduce function in javascript code
933
+ * @param query
934
+ * to match
935
+ * @return
936
+ * @throws MongoException
937
+ * @dochub mapreduce
938
+ */
939
+ public MapReduceOutput mapReduce ( String map , String reduce , String outputTarget , DBObject query ) throws MongoException {
940
+ return mapReduce ( new MapReduceCommand ( getName () , map , reduce , outputTarget , query ) );
941
+ }
933
942
934
- if ( query != null )
935
- b .add ( "query" , query );
943
+ /**
944
+ * performs a map reduce operation
945
+ * Specify an outputType (MapReduceCommand.OutputType) to control job
946
+ * execution
947
+ * * INLINE - Return results inline
948
+ * * STANDARD - Save the job output to outputTarget
949
+ * * MERGE - Merge the job output with the existing contents of outputTarget
950
+ * * REDUCE - Reduce the job output with the existing contents of
951
+ * outputTarget
952
+ *
953
+ * @param map
954
+ * map function in javascript code
955
+ * @param outputTarget
956
+ * optional - leave null if want to use temp collection
957
+ * @param outputType
958
+ * set the type of job output
959
+ * @param reduce
960
+ * reduce function in javascript code
961
+ * @param query
962
+ * to match
963
+ * @return
964
+ * @throws MongoException
965
+ * @dochub mapreduce
966
+ */
967
+ public MapReduceOutput mapReduce ( String map , String reduce , String outputTarget , MapReduceCommand .OutputType outputType , DBObject query )
968
+ throws MongoException {
969
+ return mapReduce ( new MapReduceCommand ( getName () , map , reduce , outputTarget , outputType , query ) );
970
+ }
936
971
937
- return mapReduce ( b .get () );
972
+ /**
973
+ * performs a map reduce operation
974
+ *
975
+ * @param command
976
+ * object representing the parameters
977
+ * @return
978
+ * @throws MongoException
979
+ */
980
+ public MapReduceOutput mapReduce ( MapReduceCommand command ) throws MongoException {
981
+ CommandResult res = _db .command ( command .toDBObject () );
982
+ res .throwOnError ();
983
+ return new MapReduceOutput ( this , res );
938
984
}
939
-
985
+
940
986
/**
941
987
* performs a map reduce operation
942
- * @param command object representing the parameters
988
+ *
989
+ * @param command
990
+ * object representing the parameters
943
991
* @return
944
992
* @throws MongoException
945
993
*/
946
- public MapReduceOutput mapReduce ( DBObject command )
947
- throws MongoException {
994
+ public MapReduceOutput mapReduce ( DBObject command ) throws MongoException {
948
995
if ( command .get ( "mapreduce" ) == null && command .get ( "mapReduce" ) == null )
949
996
throw new IllegalArgumentException ( "need mapreduce arg" );
950
997
CommandResult res = _db .command ( command );
0 commit comments