Need help in parsing Group operation's results in Java

34 views
Skip to first unread message

ajay

unread,
Nov 2, 2012, 10:13:02 AM11/2/12
to mongod...@googlegroups.com
I could not find Aggregation Group support in Morphia. So I am trying to use base Java driver for it. I think I got it working. But I need help in walking/parsing the results. How do you parse/walk the DBObject that is the result of group operation below?

   DBObject dbobj = col.group(key, cond, initial, "function (obj, prev) {prev.count++;}");

With corresponding Mongo shell script, this output looks like

[
        {
                "managedEntityId" : ObjectId("5092ad290cf24adee7e8a77c"),
                "severity" : "info",
                "count" : 2
        },
        {
                "managedEntityId" : ObjectId("5092ad290cf24adee7e8a77c"),
                "severity" : "minor",
                "count" : 4
        }
]

If I figure it out, I will post my results here. But if somebody already knows the answer, it will save me time.

Ajay

ajay

unread,
Nov 2, 2012, 10:38:42 AM11/2/12
to mongod...@googlegroups.com
With help from Java debugger, following works. Essentially groups returns a BasicDBList of DBObjects

BasicDBList dbList = (BasicDBList) col.group(key, cond, initial, "function (obj, prev) {prev.count++;}");

        for (Object obj : dbList) {
            DBObject dbobj = (DBObject) obj;
            for (String k: dbobj.keySet()) {
                Object o = dbobj.get(k);
            }
        }

Does it sound right?

Stephen Lee

unread,
Nov 14, 2012, 5:15:21 PM11/14/12
to mongod...@googlegroups.com
ajay, this looks right to me.  Each document should be a nested collection of sub-documents as DBObject's or DBList's.
Reply all
Reply to author
Forward
0 new messages