summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/db/commands/map_reduce_agg.cpp4
-rw-r--r--src/mongo/db/commands/map_reduce_out_options.cpp10
-rw-r--r--src/mongo/db/commands/map_reduce_out_options.h10
-rw-r--r--src/mongo/db/query/map_reduce_output_format.cpp10
-rw-r--r--src/mongo/db/query/map_reduce_output_format.h11
-rw-r--r--src/mongo/db/query/map_reduce_output_format_test.cpp14
-rw-r--r--src/mongo/s/commands/cluster_map_reduce_agg.cpp1
7 files changed, 31 insertions, 29 deletions
diff --git a/src/mongo/db/commands/map_reduce_agg.cpp b/src/mongo/db/commands/map_reduce_agg.cpp
index c94116a3934..6dd26ee46c3 100644
--- a/src/mongo/db/commands/map_reduce_agg.cpp
+++ b/src/mongo/db/commands/map_reduce_agg.cpp
@@ -115,8 +115,8 @@ bool runAggregationMapReduce(OperationContext* opCtx,
&result);
else
map_reduce_output_format::appendOutResponse(
- NamespaceString(parsedMr.getOutOptions().getDatabaseName(),
- parsedMr.getOutOptions().getCollectionName()),
+ parsedMr.getOutOptions().getDatabaseName(),
+ parsedMr.getOutOptions().getCollectionName(),
boost::get_optional_value_or(parsedMr.getVerbose(), false),
false,
&result);
diff --git a/src/mongo/db/commands/map_reduce_out_options.cpp b/src/mongo/db/commands/map_reduce_out_options.cpp
index bbcc069272b..ce82280997a 100644
--- a/src/mongo/db/commands/map_reduce_out_options.cpp
+++ b/src/mongo/db/commands/map_reduce_out_options.cpp
@@ -38,7 +38,7 @@ using namespace std::string_literals;
MapReduceOutOptions MapReduceOutOptions::parseFromBSON(const BSONElement& element) {
if (element.type() == BSONType::String) {
- return MapReduceOutOptions("", element.str(), OutputType::Replace, false);
+ return MapReduceOutOptions(boost::none, element.str(), OutputType::Replace, false);
} else if (element.type() == BSONType::Object) {
const auto obj = element.embeddedObject();
// The inline option is allowed alone.
@@ -47,7 +47,7 @@ MapReduceOutOptions MapReduceOutOptions::parseFromBSON(const BSONElement& elemen
uassert(
ErrorCodes::BadValue, "'inline' takes only numeric '1'", inMemory.number() == 1.0);
- return MapReduceOutOptions("", "", OutputType::InMemory, false);
+ return MapReduceOutOptions(boost::none, "", OutputType::InMemory, false);
}
int allowedNFields = 3;
@@ -64,15 +64,15 @@ MapReduceOutOptions MapReduceOutOptions::parseFromBSON(const BSONElement& elemen
}
}();
- const auto databaseName = [&]() {
+ const auto databaseName = [&]() -> boost::optional<std::string> {
if (const auto db = obj["db"]) {
uassert(ErrorCodes::BadValue,
"db field value must be string",
db.type() == BSONType::String);
- return db.str();
+ return boost::make_optional(db.str());
} else {
--allowedNFields;
- return ""s;
+ return boost::none;
}
}();
diff --git a/src/mongo/db/commands/map_reduce_out_options.h b/src/mongo/db/commands/map_reduce_out_options.h
index 4e99254186a..b7d25fd7840 100644
--- a/src/mongo/db/commands/map_reduce_out_options.h
+++ b/src/mongo/db/commands/map_reduce_out_options.h
@@ -52,7 +52,7 @@ public:
static MapReduceOutOptions parseFromBSON(const BSONElement& element);
MapReduceOutOptions() = default;
- MapReduceOutOptions(std::string databaseName,
+ MapReduceOutOptions(boost::optional<std::string> databaseName,
std::string collectionName,
const OutputType outputType,
bool sharded)
@@ -76,8 +76,8 @@ public:
case OutputType::Reduce:
sub.append("reduce", _collectionName);
}
- if (_databaseName != "")
- sub.append("db", _databaseName);
+ if (_databaseName)
+ sub.append("db", _databaseName.get());
if (_sharded)
sub.append("sharded", true);
}
@@ -90,12 +90,12 @@ public:
return _collectionName;
}
- const std::string& getDatabaseName() const {
+ const boost::optional<std::string>& getDatabaseName() const {
return _databaseName;
}
private:
- std::string _databaseName;
+ boost::optional<std::string> _databaseName;
std::string _collectionName;
OutputType _outputType;
bool _sharded;
diff --git a/src/mongo/db/query/map_reduce_output_format.cpp b/src/mongo/db/query/map_reduce_output_format.cpp
index c3c9859a423..9747dfa05b6 100644
--- a/src/mongo/db/query/map_reduce_output_format.cpp
+++ b/src/mongo/db/query/map_reduce_output_format.cpp
@@ -76,15 +76,15 @@ void appendInlineResponse(BSONArray&& documents,
appendMetadataFields(verbose, inMongos, resultBuilder);
}
-void appendOutResponse(NamespaceString outCollNss,
+void appendOutResponse(boost::optional<std::string> outDb,
+ std::string outColl,
bool verbose,
bool inMongos,
BSONObjBuilder* resultBuilder) {
- if (outCollNss.db().empty())
- resultBuilder->append("result", outCollNss.coll());
+ if (!outDb)
+ resultBuilder->append("result", outColl);
else
- resultBuilder->append("result",
- BSON("db" << outCollNss.db() << "collection" << outCollNss.coll()));
+ resultBuilder->append("result", BSON("db" << *outDb << "collection" << outColl));
appendMetadataFields(verbose, inMongos, resultBuilder);
}
} // namespace mongo::map_reduce_output_format
diff --git a/src/mongo/db/query/map_reduce_output_format.h b/src/mongo/db/query/map_reduce_output_format.h
index eff91ae14e3..26136efd1f1 100644
--- a/src/mongo/db/query/map_reduce_output_format.h
+++ b/src/mongo/db/query/map_reduce_output_format.h
@@ -50,12 +50,13 @@ void appendInlineResponse(BSONArray&& documents,
/**
* Appends fields to 'resultBuilder' to formulate a response that would be given if the mapReduce
- * command had written output to the collection named by 'outCollNss'. 'verbose' causes extra fields
- * to be appended to the response in accordance with the verbose option on the mapReduce command.
- * 'inMongos' indicates that we are using the format that was historically sent from mongos. If it
- * isn't set, we will use the mongod format.
+ * command had written output to the collection named by 'outColl' in output database 'outDb'.
+ * 'verbose' causes extra fields to be appended to the response in accordance with the verbose
+ * option on the mapReduce command. 'inMongos' indicates that we are using the format that was
+ * historically sent from mongos. If it isn't set, we will use the mongod format.
*/
-void appendOutResponse(NamespaceString outCollNss,
+void appendOutResponse(boost::optional<std::string> outDb,
+ std::string outColl,
bool verbose,
bool inMongos,
BSONObjBuilder* resultBuilder);
diff --git a/src/mongo/db/query/map_reduce_output_format_test.cpp b/src/mongo/db/query/map_reduce_output_format_test.cpp
index 06460770eb2..04589b4c2e3 100644
--- a/src/mongo/db/query/map_reduce_output_format_test.cpp
+++ b/src/mongo/db/query/map_reduce_output_format_test.cpp
@@ -36,6 +36,8 @@
#include "mongo/db/query/map_reduce_output_format.h"
#include "mongo/unittest/unittest.h"
+using namespace std::literals::string_literals;
+
namespace mongo {
namespace {
@@ -65,7 +67,7 @@ TEST(MapReduceOutputFormat, FormatEmptyInlineMapReduceResponse) {
TEST(MapReduceOutputFormat, FormatNonInlineMapReduceResponseWithoutDb) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("", "c"), false, false, &builder);
+ map_reduce_output_format::appendOutResponse(boost::none, "c", false, false, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: \"c\","
"timeMillis: 0,"
"counts: {input: 0, emit: 0, reduce: 0, output: 0},"
@@ -75,7 +77,7 @@ TEST(MapReduceOutputFormat, FormatNonInlineMapReduceResponseWithoutDb) {
TEST(MapReduceOutputFormat, FormatNonInlineMapReduceResponseWithDb) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("db", "c"), false, false, &builder);
+ map_reduce_output_format::appendOutResponse("db"s, "c", false, false, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: {db: \"db\", collection: \"c\"},"
"timeMillis: 0,"
"counts: {input: 0, emit: 0, reduce: 0, output: 0},"
@@ -85,7 +87,7 @@ TEST(MapReduceOutputFormat, FormatNonInlineMapReduceResponseWithDb) {
TEST(MapReduceOutputFormat, FormatVerboseMapReduceResponse) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("", "c"), true, false, &builder);
+ map_reduce_output_format::appendOutResponse(boost::none, "c", true, false, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: \"c\","
"timeMillis: 0,"
"timing: {mapTime: 0, emitLoop: 0, reduceTime: 0, total: 0},"
@@ -128,7 +130,7 @@ TEST(MapReduceOutputFormat, FormatEmptyInlineClusterMapReduceResponse) {
TEST(MapReduceOutputFormat, FormatNonInlineClusterMapReduceResponseWithoutDb) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("", "c"), false, true, &builder);
+ map_reduce_output_format::appendOutResponse(boost::none, "c", false, true, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: \"c\","
"counts: {input: 0, emit: 0, reduce: 0, output: 0},"
"timeMillis: 0,"
@@ -142,7 +144,7 @@ TEST(MapReduceOutputFormat, FormatNonInlineClusterMapReduceResponseWithoutDb) {
TEST(MapReduceOutputFormat, FormatNonInlineClusterMapReduceResponseWithDb) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("db", "c"), false, true, &builder);
+ map_reduce_output_format::appendOutResponse("db"s, "c", false, true, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: {db: \"db\", collection: \"c\"},"
"counts: {input: 0, emit: 0, reduce: 0, output: 0},"
"timeMillis: 0,"
@@ -156,7 +158,7 @@ TEST(MapReduceOutputFormat, FormatNonInlineClusterMapReduceResponseWithDb) {
TEST(MapReduceOutputFormat, FormatVerboseClusterMapReduceResponse) {
BSONObjBuilder builder;
- map_reduce_output_format::appendOutResponse(NamespaceString("", "c"), true, true, &builder);
+ map_reduce_output_format::appendOutResponse(boost::none, "c", true, true, &builder);
ASSERT_BSONOBJ_EQ(fromjson("{result: \"c\","
"counts: {input: 0, emit: 0, reduce: 0, output: 0},"
"timeMillis: 0,"
diff --git a/src/mongo/s/commands/cluster_map_reduce_agg.cpp b/src/mongo/s/commands/cluster_map_reduce_agg.cpp
index af2c92807be..e8b433956b7 100644
--- a/src/mongo/s/commands/cluster_map_reduce_agg.cpp
+++ b/src/mongo/s/commands/cluster_map_reduce_agg.cpp
@@ -75,7 +75,6 @@ bool runAggregationMapReduce(OperationContext* opCtx,
auto mrRequest = MapReduce::parse(IDLParserErrorContext("MapReduce"), cmd);
bool inMemory = mrRequest.getOutOptions().getOutputType() == OutputType::InMemory;
std::string outColl = mrRequest.getOutOptions().getCollectionName();
- std::string outDb = mrRequest.getOutOptions().getDatabaseName();
// Either inline response specified or we have an output collection.
invariant(inMemory ^ !outColl.empty());