Skip to content

Commit ec5948e

Browse files
committed
New GroupCommand class for better usability and consistency with M/R
1 parent cf8b367 commit ec5948e

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -844,33 +844,36 @@ public DBObject group( DBObject key , DBObject cond , DBObject initial , String
844844
*/
845845
public DBObject group( DBObject key , DBObject cond , DBObject initial , String reduce , String finalize )
846846
throws MongoException {
847+
GroupCommand cmd = new GroupCommand(this, key, cond, initial, reduce, finalize);
848+
return group( cmd );
849+
}
847850

848-
BasicDBObject args = new BasicDBObject();
849-
args.put( "ns" , getName() );
850-
args.put( "key" , key );
851-
args.put( "cond" , cond );
852-
args.put( "$reduce" , reduce );
853-
args.put( "initial" , initial );
854-
if ( finalize != null )
855-
args.put( "finalize" , finalize );
856-
857-
return group( args );
851+
/**
852+
* Applies a group operation
853+
* @param cmd the group command
854+
* @return
855+
* @throws MongoException
856+
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
857+
*/
858+
public DBObject group( GroupCommand cmd ) {
859+
CommandResult res = _db.command( cmd.toDBObject() );
860+
res.throwOnError();
861+
return (DBObject)res.get( "retval" );
858862
}
859863

864+
860865
/**
866+
* @deprecated prefer the {@link DBCollection#group(com.mongodb.GroupCommand)} which is more standard
861867
* Applies a group operation
862-
* @param args object representing the parameters
868+
* @param args object representing the arguments to the group function
863869
* @return
864870
* @throws MongoException
865871
* @see <a href="http://www.mongodb.org/display/DOCS/Aggregation">http://www.mongodb.org/display/DOCS/Aggregation</a>
866872
*/
867873
public DBObject group( DBObject args )
868874
throws MongoException {
869-
870-
args.put( "ns" , getName() );
871-
875+
args.put( "ns" , getName() );
872876
CommandResult res = _db.command( new BasicDBObject( "group" , args ) );
873-
874877
res.throwOnError();
875878
return (DBObject)res.get( "retval" );
876879
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (C) 2008 10gen Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb;
17+
18+
/**
19+
* This class groups the argument for a group operation and can build the underlying command object
20+
* @dochub mapreduce
21+
*/
22+
public class GroupCommand {
23+
24+
public GroupCommand(DBCollection inputCollection, DBObject keys, DBObject condition, DBObject initial, String reduce, String finalize) {
25+
this.input = inputCollection.getName();
26+
this.keys = keys;
27+
this.condition = condition;
28+
this.initial = initial;
29+
this.reduce = reduce;
30+
this.finalize = finalize;
31+
}
32+
33+
public DBObject toDBObject() {
34+
BasicDBObject args = new BasicDBObject();
35+
args.put( "ns" , input );
36+
args.put( "key" , keys );
37+
args.put( "cond" , condition );
38+
args.put( "$reduce" , reduce );
39+
args.put( "initial" , initial );
40+
if ( finalize != null )
41+
args.put( "finalize" , finalize );
42+
return new BasicDBObject( "group" , args );
43+
}
44+
45+
String input;
46+
DBObject keys;
47+
DBObject condition;
48+
DBObject initial;
49+
String reduce;
50+
String finalize;
51+
}

src/main/com/mongodb/MapReduceCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package com.mongodb;
1919

20+
/**
21+
* This class groups the argument for a map/reduce operation and can build the underlying command object
22+
* @dochub mapreduce
23+
*/
2024
public class MapReduceCommand {
2125

2226
/**

0 commit comments

Comments
 (0)