I am working on a project in which i have a huge collection in inside my mongo db. Now I have to get some details from the mongo db which are pretty much smiliar like SQL group by clause. I successfully executed the query on mongo shell :
> db.votes.aggregate(
... { $group : {
... _id : "vote_post_id",
... votesPerId : { $sum : 1 }
... }}
... );
{
"result" : [
{
"_id" : "vote_post_id",
"votesPerId" : 27371750
}
],
"ok" : 1
}
>
Now while executing the query from a java proggram I am facing follwing error :
Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command
failed [aggregate]: { "serverUsed" : "localhost/127.0.0.1:27017" ,
"errmsg" : "exception: aggregation result exceeds maximum document size (16MB)"
, "code" : 16389 , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:88)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DBCollection.aggregate(DBCollection.java:1311)
at main.Connetion.CheckConnection.GetNoOfVotesForAType(CheckConnection.java:210)
at main.DAO.Votes.VoteDAO.findKeyWord(VoteDAO.java:109)
at main.DAO.Votes.VoteDAO.main(VoteDAO.java:139)
I have debugged my java program and it is producing the exact query what I wrote on mongo shell :
{ "$group" : { "_id" : "$vote_post_id" , "count" : { "$sum" : 1}}}
I have been through a lot of answers for this problem and none seems to solve the issue.
My java code for creating the aggregation function :
// create our pipeline operations, first with the $match DBObject match = new BasicDBObject("$match", new BasicDBObject("vote_type_id", "1") );
// build the $projection operation
DBObject fields = new BasicDBObject("vote_post_id", 1);
fields.put("_id", 0);
DBObject project = new BasicDBObject("$project", fields );
// Now the $group operation
DBObject groupFields = new BasicDBObject( "_id", "$vote_post_id");
groupFields.put("count", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
AggregationOutput output = votesCollection.aggregate( match, project, group );
System.out.println(output.getCommandResult());
To make your query the same as in shell it would be:
// $group operation
DBObject groupFields = new BasicDBObject( "_id", "vote_post_id");
groupFields.put("votesPerId", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
AggregationOutput output = votesCollection.aggregate( group );
System.out.println(output.getCommandResult());
In your shell query you're doing
_id : "vote_post_id",
but in your Java query you're doing
"_id", "$vote_post_id"
The difference is the dollar sign. $vote_post_id
substitutes the value of that field to use as the bucket keys, whereas vote_post_id
just uses that literal value (so there's just one bucket).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.