Question

I'm creating an aggregation pipeline in Java using MongoDB and, of course, the MongoDB Java driver.

I've figured out how to do what I want in the Mongo Shell, but I'm not sure how to translate this into Java, particularly the whole concept of an object with commas in it, as shown below.

{$project: {sleep: {"date": 1, "hours": 1}}}

Any thoughts on the best way to do this?

Was it helpful?

Solution

Turns out one great way to do this is the following:

BasicDBObject project = new BasicDBObject("$project", new BasicDBObject("sleep", new BasicDBObject().append("date", 1).append("hours", 1)));

Then you can call your aggregation command as follows:

AggregationOutput output = collection.aggregate(commandOne, commandTwo, Project, CommandThree);

Hope this helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top