Вопрос

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?

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top