Problem Statement/Rationale
Issuing an explain on a count command for an existing collection incorrectly returns a winningPlan that includes a stage: 'EOF`. That stage typically indicates that the namespace the command is running against does not exist. Simply changing the count to a find successfully returns a plan that does a COLLSCAN (or IXSCAN if the necessary conditions are met).
Steps to Reproduce
Issue the following commands in a mongosh environment connected to a mongod:
use test db.foo.drop() db.foo.insert({}) db.foo.explain().find() db.foo.explain().count()
Expected Results
Neither explain will include a stage called "EOF".
Actual Results
The explain for the count operation incorrectly includes a stage called "EOF" and does not correctly report the actual plan that would be used for the operation.
Additional Notes
The problem appears to be that the namespace the command is executing against is incorrect. The find operation reports a queryPlanner.namespace of "test.foo" which is right. However, the count operation reports "test.test.foo" which is not correct. Creating such a collection (eg by issuing a db.getCollection('test.foo').insert({})) results in the explain for the count reporting back a valid plan (although it's still not against the namespace that the command is being issued against).
I believe the problem is this line of code:
count: `${this._collection._database._name}.${this._collection._name}`,
According to the docs, the string provided to the count parameter should be "The name of the collection or view to count." But the code above is prepending the database name to that string. This probably explains why the namespace being reported in the explain output has the database name ("test" in the example above) listed twice.
The comment a few lines above that in the code states that "This is the only one that currently lacks explicit driver support." Based on that, and at a quick glance, it seems like this is special code that probably wasn't duplicated elsewhere meaning the problem is isolated to this single command.