[JAVA-1979] GroupCommand should support key functions Created: 22/Sep/15  Updated: 15/Oct/15  Resolved: 28/Sep/15

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 3.0.4
Fix Version/s: 3.1.0, 2.14.0

Type: Improvement Priority: Major - P3
Reporter: Gordon Syme Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

com.mongodb.DBCollection#group(DBObject) is deprecated in 2.13 in favour of com.mongodb.GroupCommand and has been removed in 3.0

GroupCommand does not allow key functions to be specified and with the DBObject method removed there's no way to specify a key function to group any more.

GroupOperation does allow key functions, but because the GroupCommand#toOperation method is package protected it's not possible to create a subclass of GroupCommand that can accept key functions.

The net effect is that key functions in group operations are unavailable to JVM clients.



 Comments   
Comment by Jeffrey Yemin [ 07/Oct/15 ]

Released in 3.1.0

Comment by Githook User [ 05/Oct/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Add KeyFunction support to group in DBCollection

JAVA-1979
Branch: 2.x
https://github.com/mongodb/mongo-java-driver/commit/ac8acca9c3be00e332dee318758d1074a43bdda4

Comment by Githook User [ 28/Sep/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Add KeyFunction support to group in DBCollection

JAVA-1979
Branch: 2.14.x
https://github.com/mongodb/mongo-java-driver/commit/46a14f24034e477dc1b618b92d953acd3b30a249

Comment by Githook User [ 28/Sep/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Add KeyFunction support to group in DBCollection

JAVA-1979
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/6b2111cc4bec6669be5b9cef8b1b231908621200

Comment by Gordon Syme [ 22/Sep/15 ]

Ah, I wasn't aware of that escape-hatch, that's awesome, thanks Ross.

Comment by Ross Lawley [ 22/Sep/15 ]

Thanks gordonsyme for reporting this oversight.

Until there is a fix to GroupCommand, the only way would be to execute the command manually via db.command. See the group command documentation for details of how the group command is structured.

An example of manually executing it is below:

DB database = client.getDB("test");
DBCollection collection = database.getCollection("test");
 
collection.insert(asList(
        BasicDBObject.parse("{ _id: 0, date: \"Mon Dec 20 2010 08:51:22 GMT+0000 (UTC)\"}"),
        BasicDBObject.parse("{ _id: 1, date: \"Mon Dec 20 2010 18:51:22 GMT+0000 (UTC)\"}"),
        BasicDBObject.parse("{ _id: 2, date: \"Mon Dec 21 2010 08:51:22 GMT+0000 (UTC)\"}"),
        BasicDBObject.parse("{ _id: 3, date: \"Mon Dec 22 2010 18:51:22 GMT+0000 (UTC)\"}"),
        BasicDBObject.parse("{ _id: 4, date: \"Mon Dec 23 2010 18:51:22 GMT+0000 (UTC)\"}"),
        BasicDBObject.parse("{ _id: 5, date: \"Mon Dec 24 2010 18:51:22 GMT+0000 (UTC)\"}")));
 
String keyf = "function(doc) {"
            + "    var date = new Date(doc.date);"
            + "    var dateKey = (date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear()+'';"
            + "    return {'date': dateKey};"
            + "  }";
 
String reduce = "function(obj, prev) {"
            + "      prev.count++;"
            + "  }";
 
DBObject groupCommand = new BasicDBObject("group",
        new BasicDBObject("ns", "test")
            .append("$keyf", keyf)
            .append("initial", new BasicDBObject("count", 0))
            .append("$reduce", reduce)
);
 
CommandResult commandResult = database.command(groupCommand);
DBObject results = (DBObject) commandResult.get("retval");

Generated at Thu Feb 08 08:56:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.