java example using GroupOperation

292 views
Skip to first unread message

Marco Berri

unread,
Nov 19, 2015, 11:55:51 AM11/19/15
to mongodb-user
Hello everyone.

I would use the functionality of java .group mongo 3 you have any practical examples of how to use the class GroupOperation for perform this operations?

thank you.

my code

final MongoCollection<Document> nodesCollection = mongoDatabaseOneDms.getCollection("nodes");

BsonDocument keys = new BsonDocument("className", new BsonString("it.document"));

BsonDocument condition = new BsonDocument("stats", new BsonDocument("$exists", new BsonInt32(1)));
BasicDBObject initial = new BasicDBObject("totDownload", 0).append("totView", 0).append("totEle", 0);
BsonJavaScript reduceFunction = new BsonJavaScript("function(curr,result){}");


//THIS IS THE CAOS....
SingleServerBinding ss = new SingleServerBinding(null, null);

Codec<BsonDocument> decoder = new BsonDocumentCodec();

?? = new GroupOperation(nodesCollection, reduceFunction, Codec (????) ).key(keys).filter(condition).execute(ss);

Ross Lawley

unread,
Nov 19, 2015, 12:11:09 PM11/19/15
to mongod...@googlegroups.com
Hi Marco,

In general the aggregation framework or even the mapReduce methods should be preferred over using the Group operation.  Can you explain what problem you are trying to achieve.  I hoping that using the aggregation framework will meet your needs and be faster!

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8b71fb20-87b2-4c5f-8c5a-69a247265079%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Marco Berri

unread,
Nov 20, 2015, 1:46:08 AM11/20/15
to Unname

Hi.

This is the script.

db.nodes.group(

{key : {'className':'it.marcoberri.Document'},

cond:{"stats" : {"$exists":1}},

reduce : function(curr, result){

if(curr.stats.downloads)

result.totDownload += curr.stats.downloads;

if(curr.stats.views)

result.totView += curr.stats.views;

result.totEle++;

},

initial : {totDownload : 0, totView : 0, totEle :0}}

);

I would try the .group function in java.

Tnx.

You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/kPPt_-ygY9w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Marco Berri

unread,
Nov 20, 2015, 3:53:39 AM11/20/15
to Unname


I'd like to use the .group transcoded in java.

example script with aggregate:


db.nodes.aggregate([
{

$match : {
className : "'it.marcoberri.Document",
stats : {$exists : 1}
}
},

{    
   $group: { 
_id: null,

downloads: { 
   $sum: "$stats.downloads"
},

views: { 
   $sum: "$stats.views"
},
count: { 
   $sum: 1
},
 
   } 
}
] );

Ross Lawley

unread,
Nov 20, 2015, 7:38:20 AM11/20/15
to mongodb-user

Hi Marco,

The Java driver provides builders to make this easy! See the Filters[1] and the Aggregation[2] builder documentation for more information:

Your example using these builders would be:

  import static com.mongodb.client.model.Aggregates.group
  import static com.mongodb.client.model.Accumulators.sum
  import static com.mongodb.client.model.Filters.and
  import static com.mongodb.client.model.Filters.eq
  import static com.mongodb.client.model.Filters.exists

  collection.aggregate(asList(
          match(and(eq("className", "it.marcoberri.Document"), exists("stats"))),
          group(null, asList(
                  sum("downloads", "$stats.downloads"),
                  sum("views", "$stats.views"),
                  sum("count", 1)))
  ));

Alternatively, you could provide Json and use Document.parse()[3] helper:

  collection.aggregate(asList(
    Document.parse("{ $match : { className : \"it.marcoberri.Document\",
             stats : {$exists : 1}}}"),
    Document.parse("{ $group: { 
        _id: null,
        downloads: { $sum: \"$stats.downloads\" },
        views: {  $sum: "$stats.views" },
   count: { $sum: 1 }

Marco Berri

unread,
Nov 20, 2015, 8:32:08 AM11/20/15
to Unname

thanks , I finally used the aggregate function .

I would just like to know if the functionality of .group ( GroupOperation ) will be considered for future versions of the driver java .
It sounds very interesting !



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/kPPt_-ygY9w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.

Ross Lawley

unread,
Nov 20, 2015, 8:59:07 AM11/20/15
to mongod...@googlegroups.com
Currently, there are no plans to add it to the top level API. If you see the main documentation for MongoDB[1] you can see that alternatives are recommended (aggregation framework) because of the limitations. 

You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages