20
20
import org .bson .BsonDocumentWrapper ;
21
21
import org .bson .BsonJavaScript ;
22
22
23
+ import static com .mongodb .assertions .Assertions .notNull ;
24
+
23
25
/**
24
26
* This class groups the argument for a group operation and can build the underlying command object
25
27
*
28
30
public class GroupCommand {
29
31
private final String collectionName ;
30
32
private final DBObject keys ;
33
+ private final String keyf ;
31
34
private final DBObject condition ;
32
35
private final DBObject initial ;
33
36
private final String reduce ;
@@ -45,12 +48,38 @@ public class GroupCommand {
45
48
*/
46
49
public GroupCommand (final DBCollection collection , final DBObject keys , final DBObject condition ,
47
50
final DBObject initial , final String reduce , final String finalize ) {
51
+ notNull ("collection" , collection );
48
52
this .collectionName = collection .getName ();
49
53
this .keys = keys ;
50
54
this .condition = condition ;
51
55
this .initial = initial ;
52
56
this .reduce = reduce ;
53
57
this .finalize = finalize ;
58
+ this .keyf = null ;
59
+ }
60
+
61
+ /**
62
+ * Creates a new group command.
63
+ *
64
+ * @param collection the collection from which to perform the group by operation.
65
+ * @param keyf the function that creates a "key object" for use as the grouping key
66
+ * @param condition optional - a filter to determine which documents in the collection to process.
67
+ * @param initial the initial state of the aggregation result document.
68
+ * @param reduce a JavaScript aggregation function that operates on the documents during the grouping operation.
69
+ * @param finalize optional - a JavaScript function that runs each item in the result set before group returns the final value.
70
+ *
71
+ * @since 3.1
72
+ */
73
+ public GroupCommand (final DBCollection collection , final String keyf , final DBObject condition ,
74
+ final DBObject initial , final String reduce , final String finalize ) {
75
+ notNull ("collection" , collection );
76
+ this .collectionName = collection .getName ();
77
+ this .keyf = notNull ("keyf" , keyf );
78
+ this .condition = condition ;
79
+ this .initial = initial ;
80
+ this .reduce = reduce ;
81
+ this .finalize = finalize ;
82
+ this .keys = null ;
54
83
}
55
84
56
85
/**
@@ -59,10 +88,18 @@ public GroupCommand(final DBCollection collection, final DBObject keys, final DB
59
88
* @return a DBObject containing the group command as a MongoDB document
60
89
*/
61
90
public DBObject toDBObject () {
62
- DBObject args = new BasicDBObject ("ns" , collectionName ).append ("key" , keys )
63
- .append ("cond" , condition )
91
+ DBObject args = new BasicDBObject ("ns" , collectionName ).append ("cond" , condition )
64
92
.append ("$reduce" , reduce )
65
93
.append ("initial" , initial );
94
+
95
+ if (keys != null ) {
96
+ args .put ("key" , keys );
97
+ }
98
+
99
+ if (keyf != null ) {
100
+ args .put ("$keyf" , keyf );
101
+ }
102
+
66
103
if (finalize != null ) {
67
104
args .put ("finalize" , finalize );
68
105
}
@@ -86,6 +123,10 @@ GroupOperation<DBObject> toOperation(final MongoNamespace namespace, final DBObj
86
123
operation .key (new BsonDocumentWrapper <DBObject >(keys , codec ));
87
124
}
88
125
126
+ if (keyf != null ) {
127
+ operation .keyFunction (new BsonJavaScript (keyf ));
128
+ }
129
+
89
130
if (condition != null ) {
90
131
operation .filter (new BsonDocumentWrapper <DBObject >(condition , codec ));
91
132
}
0 commit comments