Here is my simple database:
// ----------------------------
// Collection structure for jobs
// ----------------------------
db.getCollection("jobs").drop();
db.createCollection("jobs");
db.getCollection("jobs").createIndex({
resource: NumberInt("1")
}, {
name: "resource_1"
});
// ----------------------------
// Documents of jobs
// ----------------------------
db.getCollection("jobs").insert([ {
_id: ObjectId("65aafea215315402290b215d"),
name: "Job1",
resource: ObjectId("65aafec815315402290b2160")
} ]);
db.getCollection("jobs").insert([ {
_id: ObjectId("65aafea215315402290b215e"),
name: "Job2",
resource: ObjectId("65aafec815315402290b2161")
} ]);
db.getCollection("jobs").insert([ {
_id: ObjectId("65aafea215315402290b215f"),
name: "Job3",
resource: ObjectId("65aafec815315402290b2162")
} ]);
// ----------------------------
// Collection structure for resources
// ----------------------------
db.getCollection("resources").drop();
db.createCollection("resources");
db.getCollection("resources").createIndex({
name: NumberInt("1")
}, {
name: "name_1"
});
// ----------------------------
// Documents of resources
// ----------------------------
db.getCollection("resources").insert([ {
_id: ObjectId("65aafec815315402290b2160"),
name: "Resource1"
} ]);
db.getCollection("resources").insert([ {
_id: ObjectId("65aafec815315402290b2161"),
name: "Resource2"
} ]);
db.getCollection("resources").insert([ {
_id: ObjectId("65aafec815315402290b2162"),
name: "Resource3"
} ]);
Now I would like to execute the following aggregation framework query:
db.jobs.aggregate([
{
"$lookup": {
"from": "resources",
"let": {
resourceVar: "$resource"
},
"pipeline": [
{
"$match": {
$expr: {
$eq: ["$_id", "$$resourceVar"]
},
"name": { $in: ["Resource1"] }
}
}
],
"as": "resource",
}
},
{
"$unwind": "$resource"
}
])
Everything works this way, but when executing an explain I am always getting a COLLSCAN:
{
"explainVersion": "1",
"stages": [
{
"$cursor": {
"queryPlanner": {
"namespace": "practice.jobs",
"indexFilterSet": false,
"parsedQuery": { },
"queryHash": "17830885",
"planCacheKey": "17830885",
"maxIndexedOrSolutionsReached": false,
"maxIndexedAndSolutionsReached": false,
"maxScansToExplodeReached": false,
"winningPlan": {
"stage": "COLLSCAN",
"direction": "forward"
},
"rejectedPlans": [ ]
},
"executionStats": {
"executionSuccess": true,
"nReturned": 3,
"executionTimeMillis": 6,
"totalKeysExamined": 0,
"totalDocsExamined": 3,
"executionStages": {
"stage": "COLLSCAN",
"nReturned": 3,
"executionTimeMillisEstimate": 0,
"works": 5,
"advanced": 3,
"needTime": 1,
"needYield": 0,
"saveState": 1,
"restoreState": 1,
"isEOF": 1,
"direction": "forward",
"docsExamined": 3
},
"allPlansExecution": [ ]
}
},
"nReturned": {
"$numberLong": "3"
},
"executionTimeMillisEstimate": {
"$numberLong": "0"
}
},
{
"$lookup": {
"from": "resources",
"as": "resource",
"let": {
"resourceVar": "$resource"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$resourceVar"
]
},
"name": {
"$in": [
"Resource1"
]
}
}
}
],
"unwinding": {
"preserveNullAndEmptyArrays": false
}
},
"totalDocsExamined": {
"$numberLong": "2"
},
"totalKeysExamined": {
"$numberLong": "2"
},
"collectionScans": {
"$numberLong": "0"
},
"indexesUsed": [
"_id_"
],
"nReturned": {
"$numberLong": "1"
},
"executionTimeMillisEstimate": {
"$numberLong": "4"
}
}
],
"serverInfo": {
"host": "e043d4b1d9df",
"port": 27017,
"version": "6.0.4",
"gitVersion": "44ff59461c1353638a71e710f385a566bcd2f547"
},
"serverParameters": {
"internalQueryFacetBufferSizeBytes": 104857600,
"internalQueryFacetMaxOutputDocSizeBytes": 104857600,
"internalLookupStageIntermediateDocumentMaxSizeBytes": 104857600,
"internalDocumentSourceGroupMaxMemoryBytes": 104857600,
"internalQueryMaxBlockingSortMemoryUsageBytes": 104857600,
"internalQueryProhibitBlockingMergeOnMongoS": 0,
"internalQueryMaxAddToSetBytes": 104857600,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes": 104857600
},
"command": {
"aggregate": "jobs",
"pipeline": [
{
"$lookup": {
"from": "resources",
"let": {
"resourceVar": "$resource"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$resourceVar"
]
},
"name": {
"$in": [
"Resource1"
]
}
}
}
],
"as": "resource"
}
},
{
"$unwind": "$resource"
}
],
"cursor": { },
"$db": "practice"
},
"ok": 1
}
Is there anyway to write the aggregation framework query such that it will honor the index?