Java code generated by Compass does not work

I created an aggregation in Compass version 1.33.1 and it works fine. Then I used “export to language” to generate Java code. But Java code failed with error message:
com.mongodb.MongoCommandException: Command failed with error 40323 (Location40323): ‘A pipeline stage specification object must contain exactly one field.’ on server localhost:27017. The full response is {“ok”: 0.0, “errmsg”: “A pipeline stage specification object must contain exactly one field.”, “code”: 40323, “codeName”: “Location40323”}

pipeline (worked perfect)
[{
$match: {
ME_PART_NUMBER: {
$in: [
‘72-0500-8-5003’,
‘72-0500-8-5005’
]
}
}
}, {
$project: {
ME_PART_NUMBER: 1,
TASK_TYPE: 1,
LABOR_HOURS: 1,
NUM_MECHANICS_REQUIRED: 1
}
}, {
$lookup: {
from: ‘SGE001’,
‘let’: {
part_number: ‘$ME_PART_NUMBER’
},
pipeline: [
{
$match: {
$expr: {
$eq: [
‘$ME_PART_NUMBER’,
‘$$part_number’
]
}
}
},
{
$project: {
_id: 1,
ME_PART_NUMBER: 1,
MFG_PART_NUMBER: 1,
KEYWORD_DESCRIPTION: 1
}
}
],
as: ‘SGE001_JOINED’
}
}, {}]

Generated Java code (failed):
/*

MongoClient mongoClient = new MongoClient(
new MongoClientURI(
“mongodb://localhost:27017/”
)
);
MongoDatabase database = mongoClient.getDatabase(“DTFPIMSIB”);
MongoCollection collection = database.getCollection(“SST001”);

FindIterable result = collection.aggregate(Arrays.asList(new Document("$match",
new Document(“ME_PART_NUMBER”,
new Document("$in", Arrays.asList(“72-0500-8-5003”, “72-0500-8-5005”)))),
new Document("$project",
new Document(“ME_PART_NUMBER”, 1L)
.append(“TASK_TYPE”, 1L)
.append(“LABOR_HOURS”, 1L)
.append(“NUM_MECHANICS_REQUIRED”, 1L)),
new Document("$lookup",
new Document(“from”, “SGE001”)
.append(“let”,
new Document(“part_number”, “$ME_PART_NUMBER”))
.append(“pipeline”, Arrays.asList(new Document("$match",
new Document("$expr",
new Document("$eq", Arrays.asList("$ME_PART_NUMBER", “$$part_number”)))),
new Document("$project",
new Document("_id", 1L)
.append(“ME_PART_NUMBER”, 1L)
.append(“MFG_PART_NUMBER”, 1L)
.append(“KEYWORD_DESCRIPTION”, 1L))))
.append(“as”, “SGE001_JOINED”)),
new Document()));

The provided code doesn’t compile, as it’s using FindIterable instead of AggregateIterable, but when I fix that, I can reproduce the error. The problem is that final, empty new Document() at the end of your pipeline: if you remove that then the error will go away.

Was Compass actually generating that final new Document(), or was that inadvertently added when you copied the code.? If the former, it’s potentially a Compass bug.

Let us know.

Regards,
Jeff

thanks Jeffrey for quick response. I made two minor change following your suggestion. It works perfect after that. thanks

  1. FindIterable instead of AggregateIterable. Compass generated FindIterable (possible bug?). I noticed the compilation error, and already corrected the issue before submitting here.
  2. “new Document()” is generated by Compass too. It is the root cause of my issue. but it is part of my fault. I didn’t notice there was a empty query window. After I deleted the empty window, “new Document()” is not generated - see screenshot.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.