Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-63811

mongos returns no results for $documents if the database doesn't exist

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.1.1, 6.0.3, 6.2.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Minor Change
    • ALL
    • v6.1, v6.0

      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.

            Assignee:
            gil.alon@mongodb.com Gil Alon
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: