[JAVA-2235] Multiple $match filters in BSON for aggregator Created: 29/Jun/16  Updated: 11/Sep/19  Resolved: 29/Jun/16

Status: Closed
Project: Java Driver
Component/s: API, BSON
Affects Version/s: 3.2.2
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: PRDP Assignee: Unassigned
Resolution: Done Votes: 0
Labels: driver, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Java 1.8, Java MongoDB driver v3.2.2, MongoDB 3.0.11



 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)

{ System.out.println("Testing"); }

}

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

{ "code" : 16435, "ok" : 0.0, "errmsg" : "exception: A pipeline stage specification object must contain exactly one field." }

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'
}}
]
)



 Comments   
Comment by Ross Lawley [ 29/Jun/16 ]

A pipeline stage specification object must contain exactly one field

You must only include pipeline stages that actually perform an operation - so you cannot have empty stages. Do not include empty documents in your pipeline list.

Just to let you know this project is for Java driver bugs or feature requests. The best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailing list or stackoverflow as you will reach a boarder audience there. If your business requires an answer from MongoDB within a set time frame then we do offer production support.

Generated at Thu Feb 08 08:56:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.