summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Suarez <kyle.suarez@mongodb.com>2016-12-09 13:51:35 -0500
committerKyle Suarez <kyle.suarez@mongodb.com>2016-12-09 16:42:52 -0500
commit5e7713bd86268e8a2ad4a1e41219401103681060 (patch)
treebeef35c565a8381447907009e49985858df853ce
parentaf3b0c646cc6953ac28a2b0dccd5ed16c263d424 (diff)
downloadmongo-5e7713bd86268e8a2ad4a1e41219401103681060.tar.gz
SERVER-26960 don't convert find to aggregation if sort contains $natural
-rw-r--r--jstests/core/views/views_find.js2
-rw-r--r--src/mongo/db/query/query_request.cpp10
2 files changed, 11 insertions, 1 deletions
diff --git a/jstests/core/views/views_find.js b/jstests/core/views/views_find.js
index 7d48af1e293..c196e980ce5 100644
--- a/jstests/core/views/views_find.js
+++ b/jstests/core/views/views_find.js
@@ -62,6 +62,8 @@
assertFindResultEq(
{find: "identityView", limit: 1, batchSize: 1, sort: {_id: 1}, projection: {_id: 1}},
[{_id: "New York"}]);
+ assert.commandFailedWithCode(viewsDB.runCommand({find: "identityView", sort: {$natural: 1}}),
+ ErrorCodes.InvalidPipelineOperator);
// Negative batch size and limit should fail.
assert.commandFailed(viewsDB.runCommand({find: "identityView", batchSize: -1}));
diff --git a/src/mongo/db/query/query_request.cpp b/src/mongo/db/query/query_request.cpp
index af2c46b550c..821282f78eb 100644
--- a/src/mongo/db/query/query_request.cpp
+++ b/src/mongo/db/query/query_request.cpp
@@ -99,6 +99,9 @@ const char kPartialResultsField[] = "allowPartialResults";
const char kTermField[] = "term";
const char kOptionsField[] = "options";
+// Field names for sorting options.
+const char kNaturalSortField[] = "$natural";
+
} // namespace
const char QueryRequest::kFindCommandName[] = "find";
@@ -586,7 +589,7 @@ Status QueryRequest::validate() const {
if (_tailable) {
// Tailable cursors cannot have any sort other than {$natural: 1}.
- const BSONObj expectedSort = BSON("$natural" << 1);
+ const BSONObj expectedSort = BSON(kNaturalSortField << 1);
if (!_sort.isEmpty() &&
SimpleBSONObjComparator::kInstance.evaluate(_sort != expectedSort)) {
return Status(ErrorCodes::BadValue,
@@ -981,6 +984,11 @@ StatusWith<BSONObj> QueryRequest::asAggregationCommand() const {
return {ErrorCodes::BadValue,
str::stream() << "Cannot convert to an aggregation if ntoreturn is set."};
}
+ if (_sort[kNaturalSortField]) {
+ return {ErrorCodes::InvalidPipelineOperator,
+ str::stream() << "Sort option " << kNaturalSortField
+ << " not supported in aggregation."};
+ }
// The aggregation command normally does not support the 'singleBatch' option, but we make a
// special exception if 'limit' is set to 1.
if (!_wantMore && _limit.value_or(0) != 1LL) {