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):
/*
- Requires the MongoDB Java Driver.
-
MongoDB Java Drivers
*/
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()));