[SERVER-75859] Unify behavior between mongod and mongos when running $collStats on a nonexistent database Created: 07/Apr/23  Updated: 31/Jan/24

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Kyle Suarez Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: pm3278-m1
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Execution
Participants:

 Description   

On a mongod, running a $collStats aggregation on a nonexistent database returns a basic document with information about the namespace, the host's ip, and the local machine's time. Otherwise it returns no data about the nonexistent collection. On mongos, the same aggregation results in an error.

The goal of this ticket is to unify the behavior. It is an open question what the result operation should include: host and localTime may not make sense in a mongos context as it may be routing between potentially multiple shards.



 Comments   
Comment by Mickey Winters [ 06/Dec/23 ]

repro

const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();
rst.awaitSecondaryNodes();const dbName = "nonexistant_db";let db = rst.getPrimary().getDB(dbName);
db.dropDatabase();
//let resRS = db.runCommand({explain: { aggregate: "test", cursor: {}, pipeline: [{$match: {a: 1}}, {$sort: {b: 1}}, {$group: {_id: "$c"}}]}});
let resRS = db.runCommand({ aggregate: "test", cursor: {}, pipeline: [{$collStats: {}}]});const st = new ShardingTest({shards: 2, mongo: 1, config: 1});
db = st.s0.getDB(dbName);
db.dropDatabase();
//let resSH = db.runCommand({explain: { aggregate: "test", cursor: {}, pipeline: [{$match: {a: 1}}, {$sort: {b: 1}}, {$group: {_id: "$c"}}]}});
let resSH = db.runCommand({ aggregate: "test", cursor: {}, pipeline: [{$collStats: {}}]});
print("foo");
printjson(resRS);
printjson(resSH); 

output

{
        "cursor" : {
                "firstBatch" : [
                        {
                                "ns" : "nonexistant_db.test",
                                "host" : "REDACTED:20000",
                                "localTime" : ISODate("2023-12-06T20:06:34.872Z")
                        }
                ],
                "id" : NumberLong(0),
                "ns" : "nonexistant_db.test"
        },
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1701893194, 18),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1701893194, 18)
}
{
        "ok" : 0,
        "errmsg" : "Unable to retrieve information for $collStats stage :: caused by :: database nonexistant_db not found",
        "code" : 26,
        "codeName" : "NamespaceNotFound",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1701893198, 13),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1701893198, 13)
}

However, if the DB DOES exists but the collection does not, then I get the output colin showed

Comment by Colin Stolley [ 26/Sep/23 ]

I'm not able to reproduce the issue reported, or I'm lacking some information. On master:

mongos> use config
switched to db config
mongos> db.collection.aggregate([{$collStats: {}}])
{ "ns" : "config.collection", "shard" : "config", "host" : "enzo:27010", "localTime" : ISODate("2023-09-26T17:24:47.166Z") }
mongos> db.collection.aggregate([ { "$collStats" : { "count" : { } } } ])
uncaught exception: Error: command failed: {
        "ok" : 0,
        "errmsg" : "PlanExecutor error during aggregation :: caused by :: Unable to retrieve count in $collStats stage :: caused by :: Collection [config.collection] not found.",
        "code" : 26,
        "codeName" : "NamespaceNotFound",

Now on mongod:

bart:PRIMARY> use config
switched to db config
bart:PRIMARY> db.collection.aggregate([{$collStats: {}}])
{ "ns" : "config.collection", "shard" : "bart", "host" : "enzo:27011", "localTime" : ISODate("2023-09-26T17:28:08.199Z") }
bart:PRIMARY> db.collection.aggregate([ { "$collStats" : { "count" : { } } } ])
uncaught exception: Error: command failed: {
        "ok" : 0,
        "errmsg" : "PlanExecutor error during aggregation :: caused by :: Unable to retrieve count in $collStats stage :: caused by :: Collection [config.collection] not found.",
        "code" : 26,
        "codeName" : "NamespaceNotFound",

Generated at Thu Feb 08 06:31:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.