I'm trying to convert MongoDb query using aggregation framework using Java driver. I have been helped to create query here How to apply filter for output of aggregation framework of Mongo Db?.
Here is the sample aggregate query:
db.movies.aggregate(
[{
$redact: {
$cond: {
if: {$gt: [{ $avg: "$customerReviews"}, 7 ] },
then: "$$KEEP",
else: "$$PRUNE"
}
}
},
{$skip:1},
{$limit:2}
]
);
I started with:
BasicDBObject avgQuery = new BasicDBObject("$avg", "$customerReviews");
But cannot figure out how to perform {$gt: [{ $avg: "$customerReviews"}, 7 ] }. I think it should be something like gtQuery.put(avgQuery, new BasicDbObject("$gt",7)) but obviously cannot put something other than String in put() function.
Btw, I'm not sure if $redact can be done only using BasicDbObject or I need something like Mongo spring query where two fields are equal which uses Spring Mongo. Hope someone can help me get through the whole query.