DB Version: 5.0.5
A collection has a compound key:
…createIndex
{ library: 1, collection: 1, media: 1, object: 1 },
{ name: 'components', unique: true }
)
And I want to determine if a query is covered by this index:
db.runCommand({
explain: {
find: "dlsCollection",
filter: { library: { $exists: false }, collection: { $exists: false }, media: { $exists: false }, object: { $exists: false } },
projection: { _id: true }
},
verbosity: "executionStats"
})
It would appear no documents were returned or examined (i.e. nReturned: 0, totalKeysExamined: 0, totalDocsExamined: 0):
{
explainVersion: '1',
queryPlanner: {
namespace: 'fedcDb.dlsCollection',
indexFilterSet: false,
parsedQuery: { '$and': [Array] },
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: { stage: 'EOF' },
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 0,
executionTimeMillis: 0,
totalKeysExamined: 0,
totalDocsExamined: 0,
executionStages: {
stage: 'EOF',
nReturned: 0,
executionTimeMillisEstimate: 0,
works: 1,
advanced: 0,
needTime: 0,
needYield: 0,
saveState: 0,
restoreState: 0,
isEOF: 1
}
},
command: {
find: 'dlsCollection',
filter: {
library: [Object],
collection: [Object],
media: [Object],
object: [Object]
},
projection: { _id: true },
'$db': 'fedcDb'
},
serverInfo: { edited out },
serverParameters: { edited out },
ok: 1
}
But find returns a document:
db.getCollection('dlsComponents').find(
{ library: { $exists: false }, collection: { $exists: false }, media: { $exists: false }, object: { $exists: false } },
{ _id: true }
)
{ _id: ObjectId("61e9af8bf7d71b2b030ce5e3") }
My larger goal is to create an index that will provide a covered query when it returns only _id. Other research seems to say this can be done by including the _id field as part of the compound index. I want to try that, but explain does not seem to show me when a key or document is accessed during a find command and I would want proof the query is covered before designing with the expectation a query is truly covered.
Another vein in this effort would be to remove the _id index, add a compound index that includes _id and expect mongodb to work with that, thus not duplicating the _id index. Will this work?