summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression.cpp
diff options
context:
space:
mode:
authorDavid Percy <david.percy@mongodb.com>2019-11-21 19:27:16 +0000
committerevergreen <evergreen@mongodb.com>2019-11-21 19:27:16 +0000
commit95c3c98350352fb4fe069a33efa478f3eb88eefc (patch)
treea0f4937b7362b8978327669407e74ec9feb18a02 /src/mongo/db/pipeline/expression.cpp
parent762e983eb72fa048ba00ab46c384458d308a461c (diff)
downloadmongo-95c3c98350352fb4fe069a33efa478f3eb88eefc.tar.gz
SERVER-30967 Add $binarySize expression
Diffstat (limited to 'src/mongo/db/pipeline/expression.cpp')
-rw-r--r--src/mongo/db/pipeline/expression.cpp46
1 files changed, 39 insertions, 7 deletions
diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp
index 19658f2e005..ee960534f0e 100644
--- a/src/mongo/db/pipeline/expression.cpp
+++ b/src/mongo/db/pipeline/expression.cpp
@@ -4416,20 +4416,26 @@ const char* ExpressionSubstrCP::getOpName() const {
/* ----------------------- ExpressionStrLenBytes ------------------------- */
+namespace {
+Value strLenBytes(StringData str) {
+ size_t strLen = str.size();
+
+ uassert(34470,
+ "string length could not be represented as an int.",
+ strLen <= std::numeric_limits<int>::max());
+ return Value(static_cast<int>(strLen));
+}
+} // namespace
+
Value ExpressionStrLenBytes::evaluate(const Document& root, Variables* variables) const {
Value str(_children[0]->evaluate(root, variables));
uassert(34473,
str::stream() << "$strLenBytes requires a string argument, found: "
<< typeName(str.getType()),
- str.getType() == String);
-
- size_t strLen = str.getString().size();
+ str.getType() == BSONType::String);
- uassert(34470,
- "string length could not be represented as an int.",
- strLen <= std::numeric_limits<int>::max());
- return Value(static_cast<int>(strLen));
+ return strLenBytes(str.getStringData());
}
REGISTER_EXPRESSION(strLenBytes, ExpressionStrLenBytes::parse);
@@ -4437,6 +4443,32 @@ const char* ExpressionStrLenBytes::getOpName() const {
return "$strLenBytes";
}
+/* -------------------------- ExpressionBinarySize ------------------------------ */
+
+Value ExpressionBinarySize::evaluate(const Document& root, Variables* variables) const {
+ Value arg = _children[0]->evaluate(root, variables);
+ if (arg.nullish()) {
+ return Value(BSONNULL);
+ }
+
+ uassert(51276,
+ str::stream() << "$binarySize requires a string or BinData argument, found: "
+ << typeName(arg.getType()),
+ arg.getType() == BSONType::BinData || arg.getType() == BSONType::String);
+
+ if (arg.getType() == BSONType::String) {
+ return strLenBytes(arg.getStringData());
+ }
+
+ BSONBinData binData = arg.getBinData();
+ return Value(binData.length);
+}
+
+REGISTER_EXPRESSION(binarySize, ExpressionBinarySize::parse);
+const char* ExpressionBinarySize::getOpName() const {
+ return "$binarySize";
+}
+
/* ----------------------- ExpressionStrLenCP ------------------------- */
Value ExpressionStrLenCP::evaluate(const Document& root, Variables* variables) const {