void CanonicalQuery::initSortPattern(QueryMetadataBitSet unavailableMetadata) {
    if (_qr->getSort().isEmpty()) {
        return;
    }

    // A $natural sort is really a hint, and should be handled as such. Furthermore, the downstream
    // sort handling code may not expect a $natural sort.
    //
    // We have already validated that if there is a $natural sort and a hint, that the hint
    // also specifies $natural with the same direction. Therefore, it is safe to clear the $natural
    // sort and rewrite it as a $natural hint.
    if (_qr->getSort()[QueryRequest::kNaturalSortField]) {
        _qr->setHint(_qr->getSort());
        _qr->setSort(BSONObj{});
    }

    _sortPattern = SortPattern{_qr->getSort(), _expCtx};
    _metadataDeps |= _sortPattern->metadataDeps(unavailableMetadata);

    // If the results of this query might have to be merged on a remote node, then that node might
    // need the sort key metadata. Request that the plan generates this metadata.
    if (_expCtx->needsMerge) {
        _metadataDeps.set(DocumentMetadataFields::kSortKey);
    }
}
void CanonicalQuery::initSortPattern(QueryMetadataBitSet unavailableMetadata) {
    if (_qr->getSort().isEmpty()) {
        return;
    }

     
     
     
     
     
     
    if (_qr->getSort()[QueryRequest::kNaturalSortField]) {
        _qr->setHint(_qr->getSort());
        _qr->setSort(BSONObj{});
    }

    _sortPattern = SortPattern{_qr->getSort(), _expCtx};
    _metadataDeps |= _sortPattern->metadataDeps(unavailableMetadata);

     
     
    if (_expCtx->needsMerge) {
        _metadataDeps.set(DocumentMetadataFields::kSortKey);
    }
}