summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline')
-rw-r--r--src/mongo/db/pipeline/field_path.h8
-rw-r--r--src/mongo/db/pipeline/field_path_test.cpp9
2 files changed, 17 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/field_path.h b/src/mongo/db/pipeline/field_path.h
index 0f69ebb7043..347b236fb6b 100644
--- a/src/mongo/db/pipeline/field_path.h
+++ b/src/mongo/db/pipeline/field_path.h
@@ -78,6 +78,14 @@ public:
}
/**
+ * Get the subpath including path elements [0, n].
+ */
+ StringData getSubpath(size_t n) const {
+ invariant(n + 1 < _fieldPathDotPosition.size());
+ return StringData(_fieldPath.c_str(), _fieldPathDotPosition[n + 1]);
+ }
+
+ /**
* Return the ith field name from this path using zero-based indexes.
*/
StringData getFieldName(size_t i) const {
diff --git a/src/mongo/db/pipeline/field_path_test.cpp b/src/mongo/db/pipeline/field_path_test.cpp
index 999cb9b8134..b869bc38d27 100644
--- a/src/mongo/db/pipeline/field_path_test.cpp
+++ b/src/mongo/db/pipeline/field_path_test.cpp
@@ -188,5 +188,14 @@ TEST(FieldPathTest, ConstructorAssertsOnDeeplyNestedArrayPath) {
AssertionException,
ErrorCodes::Overflow);
}
+
+// Test FieldPath::getSubpath().
+TEST(FieldPathTest, GetSubpath) {
+ FieldPath path = FieldPath("foo.bar.baz");
+ ASSERT_EQUALS("foo", path.getSubpath(0));
+ ASSERT_EQUALS("foo.bar", path.getSubpath(1));
+ ASSERT_EQUALS("foo.bar.baz", path.getSubpath(2));
+}
+
} // namespace
} // namespace mongo