4
votes

I am trying to convert a mongo aggregate query into java objects. When I am running the query in RoboMongo (tool), I get the result but converting into java objects gives empty results.

Mongo Query:

db.getCollection('wb_physicians').aggregate([
    { 
        $match: { 
            $and: [ 
                { "product.mpoCode": "VA001"}, 
                { "product.npoCode": { $exists: true } }
            ] 
        }
    },
    { 
        "$project" : { 
            "product.specialties.code": 1, 
            "providerId": 1,
            "product.code": 1, 
            "_id" : 0
        }
    }, 
    { "$unwind" : "$product.specialties" },
    { 
        "$group" : { 
            "_id" : { 
                "providerId": "$providerId" , 
                "productCode": "$product.code"
            }, 
            "specialityCodeList": { "$addToSet": "$product.specialties.code" }
        }
    }
])

Java Code:

private static AggregationOutput findProviderandSpecialty(DBCollection collection) {
    DBObject match =  new BasicDBObject("$match" , 
        new BasicDBObject("$and", Arrays.asList(
            new BasicDBObject("product.mpoCode" , "VA001").append("product.npoCode", "$exists: true")
        ))
    );

    DBObject project =  new BasicDBObject("$project" , 
        new BasicDBObject("product.specialties.code" , 1)
            .append("providerId" , 1)
            .append("product.code", 1)
            .append("_id", 0)
    );  

    DBObject unwind = new BasicDBObject("$unwind" , "$product.specialties");

    DBObject group = new BasicDBObject("$group",
        new BasicDBObject("_id", new BasicDBObject("providerId" , "$providerId"))
            .append("specialityCodeList", 
                new BasicDBObject("$addToSet", "$product.specialties.code")
            )
    );

    AggregationOutput output = collection.aggregate(match,project,unwind,group);    

    return output;
}   

Could you please help me where I made the wrong mapping?

1
+1 to the question for clearly showing both what you are trying to do and what you have tried so far; and stating what went wrong. - Vince Bowdren

1 Answers

3
votes

The problem is on the $match pipeline:

DBObject match =  new BasicDBObject("$match" , 
    new BasicDBObject("$and", Arrays.asList(
        new BasicDBObject("product.mpoCode" , "VA001")
           .append("product.npoCode", "$exists: true")
    ))
);

should be

DBObject match =  new BasicDBObject("$match" , 
    new BasicDBObject("$and", Arrays.asList(
        new BasicDBObject("product.mpoCode" , "VA001"),
        new BasicDBObject("product.npoCode", 
            new BasicDBObject("$exists", "true")
        )
    ))
);

Nonetheless, you can do without the explicit $and logic by specifying a comma-separated expression of the documents as well as removing the $project pipeline before the $group as it's rather unnecessary, so your revised pipeline could be run as:

db.getCollection('wb_physicians').aggregate([
    { 
        "$match": { 
            "product.mpoCode": "VA001", 
            "product.npoCode": { "$exists": true }             
        }
    },    
    { "$unwind" : "$product.specialties" },
    { 
        "$group" : { 
            "_id" : { 
                "providerId": "$providerId" , 
                "productCode": "$product.code"
            }, 
            "specialityCodeList": { "$addToSet": "$product.specialties.code" }
        }
    }
])

And the final Java code:

private static AggregationOutput findProviderandSpecialty(DBCollection collection) {
    DBObject match =  new BasicDBObject("$match" , 
        new BasicDBObject("product.mpoCode" , "VA001").append("product.npoCode",             
            new BasicDBObject("$exists", "true")
        )        
    );

    DBObject unwind = new BasicDBObject("$unwind" , "$product.specialties");

    DBObject group = new BasicDBObject("$group",
        new BasicDBObject("_id", new BasicDBObject("providerId" , "$providerId"))
            .append("specialityCodeList", 
                new BasicDBObject("$addToSet", "$product.specialties.code")
            )
    );

    List<DBObject> pipeline = Arrays.<DBObject>asList(match, unwind, group);
    AggregationOutput output = collection.aggregate(pipeline);    

    return output;
}