summaryrefslogtreecommitdiff
path: root/src/mongo/db/query
diff options
context:
space:
mode:
authorBernard Gorman <bernard.gorman@gmail.com>2017-01-17 20:47:21 +0000
committerJames Wahlin <james.wahlin@10gen.com>2017-01-18 12:08:17 -0500
commite8893fc311ac12028dafcf4416c2f534a42f1cf7 (patch)
tree5edfe869a5f36db525872bcdc049b39d724f7e19 /src/mongo/db/query
parent2ff3efcabb0a43d5d21b3d636caa76efa4df8bd3 (diff)
downloadmongo-e8893fc311ac12028dafcf4416c2f534a42f1cf7.tar.gz
SERVER-27438 Prevent mongos from dropping legacy $comment meta-operator
Closes #1135 Signed-off-by: James Wahlin <james.wahlin@10gen.com>
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r--src/mongo/db/query/query_request.cpp28
-rw-r--r--src/mongo/db/query/query_request.h10
-rw-r--r--src/mongo/db/query/query_request_test.cpp23
3 files changed, 59 insertions, 2 deletions
diff --git a/src/mongo/db/query/query_request.cpp b/src/mongo/db/query/query_request.cpp
index 821282f78eb..b0f1334de8d 100644
--- a/src/mongo/db/query/query_request.cpp
+++ b/src/mongo/db/query/query_request.cpp
@@ -37,6 +37,7 @@
#include "mongo/db/dbmessage.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/read_concern_args.h"
+#include "mongo/stdx/memory.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/mongoutils/str.h"
@@ -113,7 +114,7 @@ QueryRequest::QueryRequest(NamespaceString nss) : _nss(std::move(nss)) {}
StatusWith<unique_ptr<QueryRequest>> QueryRequest::makeFromFindCommand(NamespaceString nss,
const BSONObj& cmdObj,
bool isExplain) {
- unique_ptr<QueryRequest> qr(new QueryRequest(std::move(nss)));
+ auto qr = stdx::make_unique<QueryRequest>(nss);
qr->_explain = isExplain;
// Parse the command BSON by looping through one element at a time.
@@ -702,7 +703,7 @@ bool QueryRequest::isQueryIsolated(const BSONObj& query) {
// static
StatusWith<unique_ptr<QueryRequest>> QueryRequest::fromLegacyQueryMessage(const QueryMessage& qm) {
- unique_ptr<QueryRequest> qr(new QueryRequest(NamespaceString(qm.ns)));
+ auto qr = stdx::make_unique<QueryRequest>(NamespaceString(qm.ns));
Status status = qr->init(qm.ntoskip, qm.ntoreturn, qm.queryOptions, qm.query, qm.fields, true);
if (!status.isOK()) {
@@ -712,6 +713,22 @@ StatusWith<unique_ptr<QueryRequest>> QueryRequest::fromLegacyQueryMessage(const
return std::move(qr);
}
+StatusWith<unique_ptr<QueryRequest>> QueryRequest::fromLegacyQueryForTest(NamespaceString nss,
+ const BSONObj& queryObj,
+ const BSONObj& proj,
+ int ntoskip,
+ int ntoreturn,
+ int queryOptions) {
+ auto qr = stdx::make_unique<QueryRequest>(nss);
+
+ Status status = qr->init(ntoskip, ntoreturn, queryOptions, queryObj, proj, true);
+ if (!status.isOK()) {
+ return status;
+ }
+
+ return std::move(qr);
+}
+
Status QueryRequest::init(int ntoskip,
int ntoreturn,
int queryOptions,
@@ -862,6 +879,13 @@ Status QueryRequest::initFullQuery(const BSONObj& top) {
return maxTimeMS.getStatus();
}
_maxTimeMS = maxTimeMS.getValue();
+ } else if (str::equals("comment", name)) {
+ // Legacy $comment can be any BSON element. Convert to string if it isn't already.
+ if (e.type() == BSONType::String) {
+ _comment = e.str();
+ } else {
+ _comment = e.toString(false);
+ }
}
}
}
diff --git a/src/mongo/db/query/query_request.h b/src/mongo/db/query/query_request.h
index 01d356a5837..376d5ece98c 100644
--- a/src/mongo/db/query/query_request.h
+++ b/src/mongo/db/query/query_request.h
@@ -393,6 +393,16 @@ public:
*/
static StatusWith<std::unique_ptr<QueryRequest>> fromLegacyQueryMessage(const QueryMessage& qm);
+ /**
+ * Parse the provided legacy query object and parameters to construct a QueryRequest.
+ */
+ static StatusWith<std::unique_ptr<QueryRequest>> fromLegacyQueryForTest(NamespaceString nss,
+ const BSONObj& queryObj,
+ const BSONObj& proj,
+ int ntoskip,
+ int ntoreturn,
+ int queryOptions);
+
private:
Status init(int ntoskip,
int ntoreturn,
diff --git a/src/mongo/db/query/query_request_test.cpp b/src/mongo/db/query/query_request_test.cpp
index b8e45d810e4..bd40a94ef33 100644
--- a/src/mongo/db/query/query_request_test.cpp
+++ b/src/mongo/db/query/query_request_test.cpp
@@ -1304,5 +1304,28 @@ TEST(QueryRequestTest, ConvertToAggregationWithCollationSucceeds) {
ASSERT_BSONOBJ_EQ(ar.getValue().getCollation(), BSON("f" << 1));
}
+TEST(QueryRequestTest, ParseFromLegacyObjMetaOpComment) {
+ BSONObj queryObj = fromjson(
+ "{$query: {a: 1},"
+ "$comment: {b: 2, c: {d: 'ParseFromLegacyObjMetaOpComment'}}}");
+ const NamespaceString nss("test.testns");
+ unique_ptr<QueryRequest> qr(
+ assertGet(QueryRequest::fromLegacyQueryForTest(nss, queryObj, BSONObj(), 0, 0, 0)));
+
+ // Ensure that legacy comment meta-operator is parsed to a string comment
+ ASSERT_EQ(qr->getComment(), "{ b: 2, c: { d: \"ParseFromLegacyObjMetaOpComment\" } }");
+}
+
+TEST(QueryRequestTest, ParseFromLegacyStringMetaOpComment) {
+ BSONObj queryObj = fromjson(
+ "{$query: {a: 1},"
+ "$comment: 'ParseFromLegacyStringMetaOpComment'}");
+ const NamespaceString nss("test.testns");
+ unique_ptr<QueryRequest> qr(
+ assertGet(QueryRequest::fromLegacyQueryForTest(nss, queryObj, BSONObj(), 0, 0, 0)));
+
+ ASSERT_EQ(qr->getComment(), "ParseFromLegacyStringMetaOpComment");
+}
+
} // namespace mongo
} // namespace