In an attempt to allow a query to return _id without needing to access the associated documents, a compound index (components) was created using compass:
db.getCollection('dlsComponents').getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { library: 1, collection: 1, media: 1, object: 1, _id: 1 },
name: 'components',
background: false,
unique: true
}
]
The expectation is that when a query is run with a projection that includes only fields that are part of the compound index, the collection documents are not accessed/read, only the index read.
An example query is run to demonstrate expected results (data, not internal behavior):
db.getCollection('dlsComponents').find({
library: { $exists: false }, collection: { $exists: false },
media: { $exists: false }, object: { $exists: false },
_id: { $exists: true }
},
{ _id: true }
)
{ _id: ObjectId("61e9af8bf7d71b2b030ce5e3") }
The same query is run using explain, but it seems to show that a document was accessed (totalDocsExamined: 1) when no document access is expected:
db.runCommand({
explain: {
find: "dlsComponents",
filter: { library: { $exists: false }, collection: { $exists: false }, media: { $exists: false }, object: { $exists: false }, _id: { $exists: true } },
projection: { _id: true }
},
verbosity: "executionStats"
})
{
explainVersion: '1',
queryPlanner: {
namespace: 'fedcDb.dlsComponents',
indexFilterSet: false,
parsedQuery: { '$and': [Array] },
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'PROJECTION_SIMPLE',
transformBy: [Object],
inputStage: [Object]
},
rejectedPlans: [ [Object] ]
},
executionStats: {
executionSuccess: true,
nReturned: 1,
executionTimeMillis: 0,
totalKeysExamined: 1,
**totalDocsExamined: 1**,
executionStages: {
stage: 'PROJECTION_SIMPLE',
nReturned: 1,
executionTimeMillisEstimate: 0,
works: 3,
advanced: 1,
needTime: 0,
needYield: 0,
saveState: 0,
restoreState: 0,
isEOF: 1,
transformBy: [Object],
inputStage: [Object]
}
},
command: {
find: 'dlsComponents',
filter: {
library: [Object],
collection: [Object],
media: [Object],
object: [Object],
_id: [Object]
},
projection: { _id: true },
'$db': 'fedcDb'
},
serverInfo: { edited out },
serverParameters: { edited out },
ok: 1
}
Is the expectation that the documents should not be read, only the index, correct and if so, why does explain say otherwise?