summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/pipeline/document_source.h5
-rw-r--r--src/mongo/db/pipeline/document_source_lookup.cpp26
-rw-r--r--src/mongo/db/pipeline/document_source_test.cpp32
3 files changed, 31 insertions, 32 deletions
diff --git a/src/mongo/db/pipeline/document_source.h b/src/mongo/db/pipeline/document_source.h
index a2a534e7aca..99197592d23 100644
--- a/src/mongo/db/pipeline/document_source.h
+++ b/src/mongo/db/pipeline/document_source.h
@@ -1485,10 +1485,7 @@ public:
Pipeline::SourceContainer* container) final;
GetDepsReturn getDependencies(DepsTracker* deps) const final;
void dispose() final;
-
- BSONObjSet getOutputSorts() final {
- return DocumentSource::truncateSortSet(pSource->getOutputSorts(), {_as.getPath(false)});
- }
+ BSONObjSet getOutputSorts() final;
bool needsPrimaryShard() const final {
return true;
diff --git a/src/mongo/db/pipeline/document_source_lookup.cpp b/src/mongo/db/pipeline/document_source_lookup.cpp
index c4e6b72947a..a3e7f29ec77 100644
--- a/src/mongo/db/pipeline/document_source_lookup.cpp
+++ b/src/mongo/db/pipeline/document_source_lookup.cpp
@@ -326,6 +326,32 @@ BSONObj DocumentSourceLookUp::queryForInput(const Document& input,
return query.obj();
}
+BSONObjSet DocumentSourceLookUp::getOutputSorts() {
+ BSONObjSet out;
+
+ BSONObjSet inputSort = pSource->getOutputSorts();
+ std::string asPath = _as.getPath(false);
+
+ for (auto&& sortObj : inputSort) {
+ // Truncate each sortObj at the '_as' path.
+ BSONObjBuilder outputSort;
+
+ for (BSONElement fieldSort : sortObj) {
+ if (fieldSort.fieldNameStringData() == asPath) {
+ break;
+ }
+ outputSort.append(fieldSort);
+ }
+
+ BSONObj outSortObj = outputSort.obj();
+ if (!outSortObj.isEmpty()) {
+ out.insert(outSortObj);
+ }
+ }
+
+ return out;
+}
+
boost::optional<Document> DocumentSourceLookUp::unwindResult() {
const boost::optional<FieldPath> indexPath(_unwindSrc->indexPath());
diff --git a/src/mongo/db/pipeline/document_source_test.cpp b/src/mongo/db/pipeline/document_source_test.cpp
index b8f96fd1f60..c7fcdab4675 100644
--- a/src/mongo/db/pipeline/document_source_test.cpp
+++ b/src/mongo/db/pipeline/document_source_test.cpp
@@ -3352,11 +3352,11 @@ TEST(ObjectForMatch, ShouldExtractEntireArrayFromPrefixOfDottedField) {
namespace DocumentSourceLookUp {
using mongo::DocumentSourceLookUp;
-class OutputSortTruncatesOnEquality : public Mock::Base {
+class OutputSort : public Mock::Base {
public:
void run() {
intrusive_ptr<DocumentSourceMock> source = DocumentSourceMock::create();
- source->sorts = {BSON("a" << 1 << "d.e" << 1 << "c" << 1)};
+ source->sorts = {BSON("a" << 1 << "d.e" << 1)};
intrusive_ptr<DocumentSource> lookup =
DocumentSourceLookUp::createFromBson(BSON("$lookup" << BSON("from"
<< "a"
@@ -3372,30 +3372,7 @@ public:
BSONObjSet outputSort = lookup->getOutputSorts();
ASSERT_EQUALS(outputSort.count(BSON("a" << 1)), 1U);
- ASSERT_EQUALS(outputSort.size(), 1U);
- }
-};
-
-class OutputSortTruncatesOnPrefix : public Mock::Base {
-public:
- void run() {
- intrusive_ptr<DocumentSourceMock> source = DocumentSourceMock::create();
- source->sorts = {BSON("a" << 1 << "d.e" << 1 << "c" << 1)};
- intrusive_ptr<DocumentSource> lookup =
- DocumentSourceLookUp::createFromBson(BSON("$lookup" << BSON("from"
- << "a"
- << "localField"
- << "b"
- << "foreignField"
- << "c"
- << "as"
- << "d")).firstElement(),
- ctx());
- lookup->setSource(source.get());
-
- BSONObjSet outputSort = lookup->getOutputSorts();
-
- ASSERT_EQUALS(outputSort.count(BSON("a" << 1)), 1U);
+ ASSERT_EQUALS(outputSort.count(BSON("d.e" << 1)), 0U);
ASSERT_EQUALS(outputSort.size(), 1U);
}
};
@@ -3519,8 +3496,7 @@ public:
add<DocumentSourceGeoNear::LimitCoalesce>();
add<DocumentSourceGeoNear::OutputSort>();
- add<DocumentSourceLookUp::OutputSortTruncatesOnEquality>();
- add<DocumentSourceLookUp::OutputSortTruncatesOnPrefix>();
+ add<DocumentSourceLookUp::OutputSort>();
add<DocumentSourceMatch::RedactSafePortion>();
add<DocumentSourceMatch::Coalesce>();