Details
Description
I'm running with below aggregation where in match condition will be added depending upon selected filter
Bson detailsUnwind = unwind("$details");
Bson empIdMatch = new BasicDBObject();
Bson deptIdMatch = new BasicDBObject();
Bson gradeMatch = new BasicDBObject();
empIdMatch = match(eq("empId",1234567)); // mandatory value - empId
if(deptId != null) // optional value - deptId
deptIdMatch = match(eq("details.deptId",20));
if(grade != null) // optional value - grade
gradeMatch = match(eq("details.grade","L1"));
List<Bson> pipeline = asList(detailsUnwind,empIdMatch,deptIdMatch,gradeMatch);
AggregateIterable<Document> aggresult = collection.aggregate(pipeline);
for (Document documet : aggresult) {
Document doc = (Document) documet.get("details");
if (doc != null)
}
If i level is not available, than i'm getting -
Command failed with error 16435: 'exception: A pipeline stage specification object must contain exactly one field.' on server <hostname>:<port>. The full response is
This is what i want to achieve
Case 1) When both Department and Level details are available
collection.aggregate(
[
{$unwind: '$details'},
{ $match:
{'empId': 123456,
'details.deptId' : 20,
'details.grade' : 'L1'
}}
]
)
Case 2) When only Grade is available
collection.aggregate(
[
{$unwind: '$details'},
{ $match:
{'empId': 123456,
'details.grade' : 'L1'
}}
]
)