summaryrefslogtreecommitdiff
path: root/src/mongo/db
diff options
context:
space:
mode:
authorCharlie Swanson <charlie.swanson@mongodb.com>2016-10-14 15:09:59 -0400
committerCharlie Swanson <charlie.swanson@mongodb.com>2016-10-14 15:20:51 -0400
commit6966320283172b9c92556c7fe9b952dec341f9d1 (patch)
tree6d2d54667e5c06e954b43e1aa9a31ab1a8874b9c /src/mongo/db
parent96ad08051d8f8f457fe8d5b587e7dd0b3d3b379d (diff)
downloadmongo-6966320283172b9c92556c7fe9b952dec341f9d1.tar.gz
SERVER-26461 Don't exclude fields when entire document is needed.
Diffstat (limited to 'src/mongo/db')
-rw-r--r--src/mongo/db/pipeline/dependencies.cpp2
-rw-r--r--src/mongo/db/pipeline/dependencies_test.cpp27
2 files changed, 28 insertions, 1 deletions
diff --git a/src/mongo/db/pipeline/dependencies.cpp b/src/mongo/db/pipeline/dependencies.cpp
index 9a6016ed20a..bb6b7175ae5 100644
--- a/src/mongo/db/pipeline/dependencies.cpp
+++ b/src/mongo/db/pipeline/dependencies.cpp
@@ -42,7 +42,7 @@ using std::vector;
namespace str = mongoutils::str;
BSONObj DepsTracker::toProjection() const {
- if (fields.empty()) {
+ if (fields.empty() && !needWholeDocument) {
if (_needTextScore) {
// We only need the text score, but there is no easy way to express this in the query
// projection language. We use $noFieldsNeeded with a textScore meta-projection since
diff --git a/src/mongo/db/pipeline/dependencies_test.cpp b/src/mongo/db/pipeline/dependencies_test.cpp
index 9f2294d46aa..5bfd008bbfe 100644
--- a/src/mongo/db/pipeline/dependencies_test.cpp
+++ b/src/mongo/db/pipeline/dependencies_test.cpp
@@ -131,5 +131,32 @@ TEST(DependenciesToProjectionTest,
BSON(Document::metaFieldTextScore << metaTextScore << "a" << 1 << "_id" << 0));
}
+TEST(DependenciesToProjectionTest, ShouldProduceEmptyObjectIfThereAreNoDependencies) {
+ DepsTracker deps(DepsTracker::MetadataAvailable::kTextScore);
+ deps.fields = {};
+ deps.needWholeDocument = false;
+ deps.setNeedTextScore(false);
+ ASSERT_BSONOBJ_EQ(deps.toProjection(), BSONObj());
+}
+
+TEST(DependenciesToProjectionTest, ShouldAttemptToExcludeOtherFieldsIfOnlyTextScoreIsNeeded) {
+ DepsTracker deps(DepsTracker::MetadataAvailable::kTextScore);
+ deps.fields = {};
+ deps.needWholeDocument = false;
+ deps.setNeedTextScore(true);
+ ASSERT_BSONOBJ_EQ(deps.toProjection(),
+ BSON("_id" << 0 << "$noFieldsNeeded" << 1 << Document::metaFieldTextScore
+ << metaTextScore));
+}
+
+TEST(DependenciesToProjectionTest,
+ ShouldRequireTextScoreIfNoFieldsPresentButWholeDocumentIsNeeded) {
+ DepsTracker deps(DepsTracker::MetadataAvailable::kTextScore);
+ deps.fields = {};
+ deps.needWholeDocument = true;
+ deps.setNeedTextScore(true);
+ ASSERT_BSONOBJ_EQ(deps.toProjection(), BSON(Document::metaFieldTextScore << metaTextScore));
+}
+
} // namespace
} // namespace mongo