user1965449
user1965449

Reputation: 2931

Translating MongoDB query to a MongoDB java driver query

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"} } }
)

Upvotes: 2

Views: 2389

Answers (2)

matfur92
matfur92

Reputation: 330

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);

Upvotes: 1

erdimeola
erdimeola

Reputation: 854

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();

Upvotes: 4

Related Questions