4

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
    +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. Commented Apr 4, 2017 at 12:41

1 Answer 1

3

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;
}   
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response.I got the results.When i am trying to iterate the Aggregation output getting null poniter for "providerId" for (DBObject ldocFromDB : cursorDoc.results()) { DBObject providerIds = (DBObject) ldocFromDB.get(PROVIDER_ID); BasicDBList specialtyList = (BasicDBList) ldocFromDB.get("specialityCodeList"); } but got the result for BasicDBList values. could you please help me for how to get the DBObject values. Here is the output "_id" : { "providerId" : "3310100", "productCode" : "CONNECT" }, "specialityCodeList" : [ "OP" ] }
Looks like Converting Mongo aggregate query into java objects is solved isn't it? As for the other problem, that would be a different question, consider posting it separately.

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.