Aggregation framework example on java driver

1,699 views
Skip to first unread message

Martinus Martinus

unread,
Mar 11, 2012, 10:28:46 PM3/11/12
to mongod...@googlegroups.com
Hi,

Is there any link or example of how to use aggregation framework on Java driver?

Thanks.

Akbar Gadhiya

unread,
Mar 12, 2012, 2:33:01 AM3/12/12
to mongod...@googlegroups.com
Hi,

There is an example give here, http://www.mongodb.org/display/DOCS/Java+Tutorial

But my requirement is to perform group by for following query,

SELECT
up.user_name,
up.created_time,
up.created_ip,
c.company_name
FROM user_profile up, company_profile c
WHERE up.user_company = c.company_id
GROUP BY up.user_name, up.created_time, up.created_ip, c.company_name

I am not sure how to perform group by this way.

Can anybody please help me in this to do through java?

Thanks,
Akbar.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Randolph Tan

unread,
Mar 12, 2012, 1:19:15 PM3/12/12
to mongodb-user
Unfortunately, the query you are trying to do involves a join. The
aggregation command can only work on one collection at a time as of
the moment. You can either change your schema so you don't need to
perform a join, or do the query twice and do the join manually (which
is less recommended).

On Mar 12, 2:33 am, Akbar Gadhiya <akbar.gadh...@gmail.com> wrote:
> Hi,
>
> There is an example give here,http://www.mongodb.org/display/DOCS/Java+Tutorial
>
> But my requirement is to perform group by for following query,
>
> SELECT
> up.user_name,
> up.created_time,
> up.created_ip,
> c.company_name
> FROM user_profile up, company_profile c
> WHERE up.user_company = c.company_id
> GROUP BY up.user_name, up.created_time, up.created_ip, c.company_name
>
> I am not sure how to perform group by this way.
>
> Can anybody please help me in this to do through java?
>
> Thanks,
> Akbar.
>

Randolph Tan

unread,
Mar 12, 2012, 2:28:16 PM3/12/12
to mongodb-user
I don't know any sites, but I can provide you with an example. It is
similar to calling commands (you can also check the docs page for more
details - http://www.mongodb.org/display/DOCS/Aggregation+Framework):

Mongo m = new Mongo("localhost", 27017);
 DB db = m.getDB("test");
 BasicDBObject cmdBody = new BasicDBObject("aggregate", "user");
      ArrayList<BasicDBObject> pipeline = new
ArrayList<BasicDBObject>();

      {
        BasicDBObject projectParam = new BasicDBObject("name", 1);
        projectParam.put("state", 1);
        pipeline.add(new BasicDBObject("$project", projectParam));
      }

      pipeline.add(new BasicDBObject("$limit", 5));

      cmdBody.put("pipeline", pipeline);
      System.out.println("result: " + db.command(cmdBody));

Akbar Gadhiya

unread,
Mar 13, 2012, 12:06:19 AM3/13/12
to mongod...@googlegroups.com
Hello Randolph,

Thank you for quick response.

I agree with you to remove joins. But lets say I want to apply group by in one table only and my requirement is to get a cursor object.

Like,

criteria = constructCriteria();
DBObject sort = getSorting();
DBCursor cursor = collection.find(criteria).sort(sort);

What I found to perform group by is collection.group(......) API which returns DBObject.

Is there any way I can get cursor over collection with grouping?

Thanks,
Akbar.


Randolph Tan

unread,
Mar 13, 2012, 12:38:52 PM3/13/12
to mongodb-user
Unfortunately, both group and the aggregration framework are built on
top of the command framework, which gives you a document as a result.
There's an ongoing ticket (https://jira.mongodb.org/browse/
SERVER-3253) for implementing the $out option, which can potentially
output the result to a collection, then you can query on it and get a
cursor.

On Mar 13, 12:06 am, Akbar Gadhiya <akbar.gadh...@gmail.com> wrote:
> Hello Randolph,
>
> Thank you for quick response.
>
> I agree with you to remove joins. But lets say I want to apply group by in
> one table only and my requirement is to get a cursor object.
>
> Like,
>
> criteria = constructCriteria();
> DBObject sort = getSorting();
> DBCursor cursor = collection.find(criteria).sort(sort);
>
> What I found to perform group by is collection.group(......) API which
> returns DBObject.
>
> Is there any way I can get cursor over collection with grouping?
>
> Thanks,
> Akbar.
>
>
>
>
>
>
>
> On Mon, Mar 12, 2012 at 11:58 PM, Randolph Tan <rando...@10gen.com> wrote:
> > I don't know any sites, but I can provide you with an example. It is
> > similar to calling commands (you can also check the docs page for more
> > details -http://www.mongodb.org/display/DOCS/Aggregation+Framework):
Reply all
Reply to author
Forward
0 new messages