Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform aggregation with MongoDB 3.0 java driver

Tags:

java

mongodb

I am trying to perform simple aggregation operation using MongoDB java driver (3.0). Help required on how to write the below mongo shell query using JAVA API.

db.coll.aggregate([
    { $project: {
        email_id: { $ifNull: [ "$email_id", " " ] },
        phone_num: { $ifNull: [ "$phone_num","NA" ] },
        id : 1,
        firstname :1,
        lastname :1,
        status : 1
        }
    },
    { $match: { status: "true"} },
    { "$group": {
        "_id": "$id",
        "details": { "$push": {
            "$concat": [ "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num" ]
        }}
    }}
])
like image 435
gsuresh92 Avatar asked Dec 08 '25 10:12

gsuresh92


1 Answers

Here are the java driver queries for your shell queries

MongoClient mongo = new MongoClient();    
MongoDatabase database = mongo.getDatabase("your_database");
        Document emailIdDoc = new Document("$ifNull",Arrays.asList("$email_id", " "));
        Document phoneNumDoc = new Document("$ifNull", Arrays.asList("$phone_num", "NA")); 
        Document projectDoc = new Document("$project" , new Document("email_id", emailIdDoc).append("phone_num", phoneNumDoc).append("id" , 1).append("firstname" , 1).append("lastname", 1).append("status", 1));
        Document matchDoc = new Document("$match", new BasicDBObject("status", "true"));
        Document groupDoc = new Document("$group", new Document("_id", "$id").append("details", new Document("$push", concat)));
        AggregateIterable<Document> aggregationResult = db.getCollection("your_collection").aggregate(asList(projectDoc, matchDoc, groupDoc));

With Deprecated methods

MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("your_database");
    BasicDBObject emailId = new BasicDBObject("$ifNull",Arrays.asList("$email_id", " "));
    BasicDBObject phoneNum = new BasicDBObject("$ifNull", Arrays.asList("$phone_num", "NA"));
    DBObject project = new BasicDBObject("$project" , new BasicDBObject("email_id", emailId).append("phone_num", phoneNum).append("id" , 1).append("firstname" , 1).append("lastname", 1).append("status", 1));
    DBObject match = new BasicDBObject("$match", new BasicDBObject("status", "true")); 
    DBObject concat = new BasicDBObject("$concat", Arrays.asList( "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num"));
    Document concatDoc = new Document("$concat", Arrays.asList( "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num"));
    DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$id").append("details", new BasicDBObject("$push", concat)));

    AggregationOutput iterable = db.getCollection("your_collection").aggregate(project, match, group);
like image 178
Clement Amarnath Avatar answered Dec 10 '25 07:12

Clement Amarnath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!