diff options
author | Charlie Swanson <charlie.swanson@mongodb.com> | 2018-02-28 17:20:12 -0500 |
---|---|---|
committer | Charlie Swanson <charlie.swanson@mongodb.com> | 2018-03-01 10:35:28 -0500 |
commit | 2a4328dca297c8c1f76dafcff958c75703881207 (patch) | |
tree | 5dcaa7d066da90b8b73b226c9d7250d09ef8bee7 /src/mongo/db/pipeline/expression.cpp | |
parent | bf2c0083968557be95316d083ab7c8fa896016d7 (diff) | |
download | mongo-2a4328dca297c8c1f76dafcff958c75703881207.tar.gz |
SERVER-33510 Add shorthand syntax for $convert
Diffstat (limited to 'src/mongo/db/pipeline/expression.cpp')
-rw-r--r-- | src/mongo/db/pipeline/expression.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp index 00b6a83cafe..a0a6b990dae 100644 --- a/src/mongo/db/pipeline/expression.cpp +++ b/src/mongo/db/pipeline/expression.cpp @@ -5230,6 +5230,22 @@ private: } }; +Expression::Parser makeConversionAlias(const StringData shortcutName, BSONType toType) { + return [=](const intrusive_ptr<ExpressionContext>& expCtx, + BSONElement elem, + const VariablesParseState& vps) -> intrusive_ptr<Expression> { + + // Use parseArguments to allow for a singleton array, or the unwrapped version. + auto operands = ExpressionNary::parseArguments(expCtx, elem, vps); + + uassert(50723, + str::stream() << shortcutName << " requires a single argument, got " + << operands.size(), + operands.size() == 1); + return ExpressionConvert::create(expCtx, operands[0], toType); + }; +} + } // namespace const double ExpressionConvert::kLongLongMaxPlusOneAsDouble = @@ -5240,6 +5256,55 @@ REGISTER_EXPRESSION_WITH_MIN_VERSION( ExpressionConvert::parse, ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +// Also register shortcut expressions like $toInt, $toString, etc. which can be used as a shortcut +// for $convert without an 'onNull' or 'onError'. +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toString, + makeConversionAlias("$toString"_sd, BSONType::String), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toObjectId, + makeConversionAlias("$toObjectId"_sd, BSONType::jstOID), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toDate, + makeConversionAlias("$toDate"_sd, BSONType::Date), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toDouble, + makeConversionAlias("$toDouble"_sd, BSONType::NumberDouble), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toInt, + makeConversionAlias("$toInt"_sd, BSONType::NumberInt), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toLong, + makeConversionAlias("$toLong"_sd, BSONType::NumberLong), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toDecimal, + makeConversionAlias("$toDecimal"_sd, BSONType::NumberDecimal), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); +REGISTER_EXPRESSION_WITH_MIN_VERSION( + toBool, + makeConversionAlias("$toBool"_sd, BSONType::Bool), + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40); + +boost::intrusive_ptr<Expression> ExpressionConvert::create( + const boost::intrusive_ptr<ExpressionContext>& expCtx, + const boost::intrusive_ptr<Expression>& input, + BSONType toType) { + return new ExpressionConvert(expCtx, input, toType); +} + +ExpressionConvert::ExpressionConvert(const boost::intrusive_ptr<ExpressionContext>& expCtx, + const boost::intrusive_ptr<Expression>& input, + BSONType toType) + : Expression(expCtx), + _input(input), + _to(ExpressionConstant::create(expCtx, Value(StringData(typeName(toType))))) {} + intrusive_ptr<Expression> ExpressionConvert::parse( const boost::intrusive_ptr<ExpressionContext>& expCtx, BSONElement expr, |