[SERVER-63811] mongos returns no results for $documents if the database doesn't exist Created: 17/Feb/22  Updated: 29/Oct/23  Resolved: 22/Sep/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.1.1, 6.0.3, 6.2.0-rc0

Type: Bug Priority: Major - P3
Reporter: Charlie Swanson Assignee: Gil Alon
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Duplicate
is duplicated by SERVER-71728 $documents not working on sharded clu... Closed
Related
related to SERVER-69836 mongos doesn't raise validation error... Closed
related to SERVER-70176 $documents sharding tests runs on ver... Closed
is related to SERVER-63793 Add test demonstrating behavior of $s... Closed
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v6.1, v6.0
Participants:

 Description   

A mongos will return an empty result set on any pipeline with a $documents stage like this one:

db.aggregate([{$documents: [{_id: 1}, {_id: 2}]}]);

I believe this is due to this logic:

    // If the routing table is valid, we obtain a reference to it. If the table is not valid, then
    // either the database does not exist, or there are no shards in the cluster. In the latter
    // case, we always return an empty cursor. In the former case, if the requested aggregation is a
    // $changeStream, we allow the operation to continue so that stream cursors can be established
    // on the given namespace before the database or collection is actually created. If the database
    // does not exist and this is not a $changeStream, then we return an empty cursor.
    boost::optional<ChunkManager> cm;
    auto executionNsRoutingInfoStatus =
        sharded_agg_helpers::getExecutionNsRoutingInfo(opCtx, namespaces.executionNss);
 
    if (!executionNsRoutingInfoStatus.isOK()) {
        if (liteParsedPipeline.startsWithCollStats()) {
            uassertStatusOKWithContext(executionNsRoutingInfoStatus,
                                       "Unable to retrieve information for $collStats stage");
        }
    }
 
    if (executionNsRoutingInfoStatus.isOK()) {
        cm = std::move(executionNsRoutingInfoStatus.getValue());
    } else if (!(hasChangeStream &&
                 executionNsRoutingInfoStatus == ErrorCodes::NamespaceNotFound)) {
        appendEmptyResultSetWithStatus(
            opCtx, namespaces.requestedNss, executionNsRoutingInfoStatus.getStatus(), result);
        return Status::OK();
    }

This is a behavioral difference from a replica set, and can make writing tests annoying (discovered in SERVER-63793).

The new, requested behavior is to allow the operation to continue even if the db doesn't exist for $documents. Similar to how $changeStreams is handled, it may be the case that special support/case handling will have to be added to support $documents if current agg implementation isn't prepared to handle the db not existing.



 Comments   
Comment by Githook User [ 02/Jun/23 ]

Author:

{'name': 'Anna Henningsen', 'email': 'anna.henningsen@mongodb.com', 'username': 'addaleax'}

Message: fix(shell-api): try running convertShardKeyToHashed agg against empty db (#1479)

Recently, our serverless connectivity tests have been failing in CI because
none of the approaches in the `convertShardKeyToHashed()` were
working anymore.

However, our serverless test instance is now running MongoDB 6.3.1,
which contains the fix for SERVER-63811; adding a new variant
that runs the pipeline against a fake/empty database seems to work
now, and is probably the most preferable approach anyway.
Branch: search-index-helpers
https://github.com/mongodb-js/mongosh/commit/056479985abee14d09003e6f5a87e88b628bd418

Comment by Githook User [ 01/Jun/23 ]

Author:

{'name': 'Anna Henningsen', 'email': 'anna.henningsen@mongodb.com', 'username': 'addaleax'}

Message: fix(shell-api): try running convertShardKeyToHashed agg against empty db (#1479)

Recently, our serverless connectivity tests have been failing in CI because
none of the approaches in the `convertShardKeyToHashed()` were
working anymore.

However, our serverless test instance is now running MongoDB 6.3.1,
which contains the fix for SERVER-63811; adding a new variant
that runs the pipeline against a fake/empty database seems to work
now, and is probably the most preferable approach anyway.
Branch: main
https://github.com/mongodb-js/mongosh/commit/056479985abee14d09003e6f5a87e88b628bd418

Comment by Githook User [ 01/Jun/23 ]

Author:

{'name': 'Anna Henningsen', 'email': 'anna.henningsen@mongodb.com', 'username': 'addaleax'}

Message: fix(shell-api): try running convertShardKeyToHashed agg against empty db

Recently, our serverless connectivity tests have been failing in CI because
none of the approaches in the `convertShardKeyToHashed()` were
working anymore.

However, our serverless test instance is now running MongoDB 6.3.1,
which contains the fix for SERVER-63811; adding a new variant
that runs the pipeline against a fake/empty database seems to work
now, and is probably the most preferable approach anyway.
Branch: cskth-serverless
https://github.com/mongodb-js/mongosh/commit/84cc16933f88356fc4c45c8e5e74010fc31b9d9f

Comment by Githook User [ 14/Oct/22 ]

Author:

{'name': 'galon1', 'email': 'gil.alon@mongodb.com', 'username': 'galon1'}

Message: SERVER-63811 Add check so documents stage runs when db does not exist
Branch: v6.0
https://github.com/mongodb/mongo/commit/199173d18b9af33156290967dc50d1af3e0c34b3

Comment by Githook User [ 10/Oct/22 ]

Author:

{'name': 'galon1', 'email': 'gil.alon@mongodb.com', 'username': 'galon1'}

Message: SERVER-63811 Add check to allow documents stage to run without a database existing
Branch: v6.1
https://github.com/mongodb/mongo/commit/cc7969f8f50d5095f66b0e15069b46e2512f70f2

Comment by Githook User [ 20/Sep/22 ]

Author:

{'name': 'galon1', 'email': 'gil.alon@mongodb.com', 'username': 'galon1'}

Message: SERVER-63811 Add check to allow documents stage to run without a database existing
Branch: master
https://github.com/mongodb/mongo/commit/4f0c417801b6aa0e478675220975d6dc92d45233

Generated at Thu Feb 08 05:58:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.