Expected covered query shows documents were examined

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?

Update:

  • I tried replacing _id with a different field in case mongoDB had a issue with _id in the compound index, but it still did not cover the query.

  • I tried using hint in the query options, to point it to the index, still reading the document

  • I tried a query without the ( $exists: } and it did not read the document. The documentation says you can’t do covered queries with null fields, but it does not say you can not have missing { $exists: false } fields. This sounds like a documentation issue.

Says: no fields in the query are equal to null (i.e. { "field" : null } or { "field" : {$eq : null}} ),

but does not exclude missing { $exists: false } fields

the fields in this issue were missing (not present in the document), not null.

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