summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/dependencies.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline/dependencies.cpp')
-rw-r--r--src/mongo/db/pipeline/dependencies.cpp50
1 files changed, 28 insertions, 22 deletions
diff --git a/src/mongo/db/pipeline/dependencies.cpp b/src/mongo/db/pipeline/dependencies.cpp
index bb6b7175ae5..a1fc3b88a19 100644
--- a/src/mongo/db/pipeline/dependencies.cpp
+++ b/src/mongo/db/pipeline/dependencies.cpp
@@ -41,34 +41,40 @@ using std::vector;
namespace str = mongoutils::str;
-BSONObj DepsTracker::toProjection() const {
- 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
- // this is an inclusion projection which will exclude all existing fields but add the
- // textScore metadata.
- return BSON("_id" << 0 << "$noFieldsNeeded" << 1 << Document::metaFieldTextScore
- << BSON("$meta"
- << "textScore"));
- } else {
- // We truly need no information (we are doing a count or something similar). In this
- // case, the DocumentSourceCursor will know there aren't any dependencies, and we can
- // ignore the documents returned from the query system. We pass an empty object as the
- // projection so that we have a chance of using the COUNT_SCAN optimization.
- return BSONObj();
- }
+bool DepsTracker::_appendMetaProjections(BSONObjBuilder* projectionBuilder) const {
+ if (_needTextScore) {
+ projectionBuilder->append(Document::metaFieldTextScore,
+ BSON("$meta"
+ << "textScore"));
}
+ if (_needSortKey) {
+ projectionBuilder->append(Document::metaFieldSortKey,
+ BSON("$meta"
+ << "sortKey"));
+ }
+ return (_needTextScore || _needSortKey);
+}
+BSONObj DepsTracker::toProjection() const {
BSONObjBuilder bb;
- if (_needTextScore)
- bb.append(Document::metaFieldTextScore,
- BSON("$meta"
- << "textScore"));
+ const bool needsMetadata = _appendMetaProjections(&bb);
- if (needWholeDocument)
+ if (needWholeDocument) {
return bb.obj();
+ }
+
+ if (fields.empty()) {
+ if (needsMetadata) {
+ // We only need metadata, but there is no easy way to express this in the query
+ // projection language. We use $noFieldsNeeded with a meta-projection since this is an
+ // inclusion projection which will exclude all existing fields but add the metadata.
+ bb.append("_id", 0);
+ bb.append("$noFieldsNeeded", 1);
+ }
+ // We either need nothing (as we would if this was logically a count), or only the metadata.
+ return bb.obj();
+ }
bool needId = false;
string last;