mongodb grouping and subgrouping with java driver -
i need query mongodb db in such way kind of data hierarchy grouping field1
, field2
. shell can run following:
db.collection.aggregate([{'$group': {'count': {'$sum': 1}, '_id': {'field1': '$field1', 'field2': '$field2'}}}])
does did same java?
it better if provide sample document.
you can convert query mongo-java
driver following:
dbcollection collection = db.getcollection("collectionname"); dbobject groupfieldsinid = new basicdbobject(); groupfieldsinid.put( "field1", "$field1"); groupfieldsinid.put( "field2", "$field2"); dbobject groupfields = new basicdbobject( "_id", groupfieldsinid); groupfields.put("count", new basicdbobject( "$sum", 1)); dbobject group = new basicdbobject("$group", groupfields ); aggregationoutput output = collection.aggregate(group);
Comments
Post a Comment