Can someone help me telling if the java code for the following query using Aggregates is correct or not?
Mongo DB Shell Query:
db.getCollection('xyz').aggregate([
{
$match: {
"try.Ind": "A",
"_id.a": "AC",
"_id.b": "HXYZ",
"_id.c": "12345678",
"_id.d": "AA",
"_id.e": "2018-03-09"
}
},
{
$project: {
"try": 1,
total: {
$sum: {
$map: {
input: {
$filter: {
input: "$try",
cond: {
$and: [
{
$eq: [
"$$this.Ind",
"A"
]
},
{
$lt: [
"$$this.mt",
0.0
]
}
]
}
}
},
"in": "$$this.mt"
}
}
}
}
}
])
Java Code:
I have used the Bson filter inorder to match all my conditions in the query.
Bson filter = and(eq("try.Ind","A"),eq("_id.a","AC"),eq ("_id.b","HXYZ"),eq("_id.c","12345678"),eq("_id.d","AA"),eq("_id.e","2018-03-09"));
AggregateIterable<Document> aggDocument=collectionName.aggregate(
Arrays.asList(
Aggregates.match(filter),
Aggregates.project(
Projections.fields(Projections.include("try"),
new Document("total",
new Document("$sum",
new Document("$map",
new Document("input",
new Document("$filter",
new Document("input","$try").
append ("cond",new Document("$and",Arrays.asList(
new Document("$eq",Arrays.asList("$$this.Ind","A")),
new Document("$lt",Arrays.asList(
"$$this.mt",0.0))))))).append("in","$$this.mt"))))))));
Please help me correcting if anything is wrong in the above code. Thanks in advance!!