2

Please help me translate the following MongoDB query to a java based Query using the Java MongoDB driver. Thank You.

db.playerscorecollection.aggregate(
      { $unwind: "$scorearray"},
      { $group: { _id: { player: "$player", venue: "$scorearray.venue", score: "$scorearray.score" } } },
      { $sort: { "_id.score" : 1 } },
      { $group: { _id: "$_id.player", maxScore: { $last: "$_id.score" }, venue: { $last: "$_id.venue"} } }
)
1
  • It is rare we answer questions that ask for code with out research, this time you was kind of lucky, normally these questions would be closed. Commented Jul 13, 2013 at 10:40

2 Answers 2

4

I haven't checked the syntax. Also I don't know if your query works or not but here's my try.

        //unwind
        DBObject unwind = new BasicDBObject("$unwind", "$scorearray");
        // Now the $group operation
        DBObject groupFields = new BasicDBObject("player", "$player");
        groupFields.put("venue", "$scorearray.venue"));
        groupFields.put("score", "$scorearray.score"));
        DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", groupFields));
        //sort
        DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id.score":1));
        //secondgroup
        DBObject secondGroupFields = new BasicDBObject("_id", "$_id.player")
        secondGroupFields.put("maxScore", new BasicDBObject("$last":"$_id.score"));
        secondGroupFields.put("venue", new BasicDBObject("$last":"$_id.venue"));
        DBObject secondGroup = new BasicDBObject("$group", secondGroupFields);

        // run aggregation
        AggregationOutput output = playerScoreCollection.aggregate(unwind, group,sort,secondGroup);

        Iterable<DBObject> result = output.results();
        Iterator<DBObject> iterator = result.iterator();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You ! Yes my query is a working one and your solution works too.
1

You can use this library: https://github.com/EqualExperts/mongo-shell-like-query With this library you can use query string like:

db.users.aggregate([{'$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'age': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 }])

or

db.users.aggregate({'$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'age': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 })

passing these strings like this example: String query = ”db.users.find( { ‘name’ : ‘John’} )”; MongoQueryParser parser = new MongoQueryParser(); MongoQuery mongoQuery = parser.parse(query, new HashMap()); BasicDBList results = mongoQuery.execute(mongoDB); It's very very fast to integrate and use in my opinion.

In alternative there is another fantastic library:

http://jongo.org

With this you can use code like:

DB db = new MongoClient().getDB("dbname");
Jongo jongo = new Jongo(db);
MongoCollection friends = jongo.getCollection("friends");
friends.aggregate("{$project:{sender:1}}")
    .and("{$match:{tags:'read'}}")
    .and("{$limit:10}")
    .as(Email.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.