summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Wahlin <james@mongodb.com>2019-05-07 12:30:07 -0400
committerJames Wahlin <james@mongodb.com>2019-05-08 13:02:13 -0400
commit2c7681d8c20af6ba8ffc97fe63337ce33a6eb435 (patch)
tree447d15a62da6aa61adc8a78482ae8dbe4e8ae782
parentf25d7d70ca9b2c0282640779adf5c690c0368f1b (diff)
downloadmongo-2c7681d8c20af6ba8ffc97fe63337ce33a6eb435.tar.gz
SERVER-40405 Add support for 'sort' option in combination with a pipeline-style findAndModify
-rw-r--r--jstests/core/find_and_modify_pipeline_update.js19
-rw-r--r--src/mongo/db/query/find_and_modify_request.cpp11
2 files changed, 17 insertions, 13 deletions
diff --git a/jstests/core/find_and_modify_pipeline_update.js b/jstests/core/find_and_modify_pipeline_update.js
index 298c5cc1788..d97ace94777 100644
--- a/jstests/core/find_and_modify_pipeline_update.js
+++ b/jstests/core/find_and_modify_pipeline_update.js
@@ -5,6 +5,8 @@
(function() {
"use strict";
+ load("jstests/libs/fixture_helpers.js"); // For isMongos.
+
const coll = db.find_and_modify_pipeline_update;
coll.drop();
@@ -33,14 +35,21 @@
{query: {_id: 3}, update: [{$addFields: {y: 3}}], fields: {x: 1}, new: true});
assert.eq(found, {_id: 3, x: 3});
+ // We skip the following test for sharded fixtures as it will fail as the query for
+ // findAndModify must contain the shard key.
+ if (!FixtureHelpers.isMongos(db)) {
+ // Test that 'sort' works with pipeline-style update.
+ assert(coll.drop());
+ assert.commandWorked(
+ coll.insert([{_id: 0, x: 'b'}, {_id: 1, x: 'd'}, {_id: 2, x: 'a'}, {_id: 3, x: 'c'}]));
+ found =
+ coll.findAndModify({update: [{$addFields: {foo: "bar"}}], sort: {x: -1}, new: true});
+ assert.eq(found, {_id: 1, x: 'd', foo: "bar"});
+ }
+
// Test that it rejects the combination of arrayFilters and a pipeline-style update.
let err = assert.throws(
() => coll.findAndModify(
{query: {_id: 1}, update: [{$addFields: {y: 1}}], arrayFilters: [{"i.x": 4}]}));
assert.eq(err.code, ErrorCodes.FailedToParse);
-
- // SERVER-40405 Add support for sort.
- err = assert.throws(() => coll.findAndModify(
- {query: {_id: 1}, update: [{$addFields: {y: 1}}], sort: {_id: -1}}));
- assert.eq(err.code, ErrorCodes.NotImplemented);
}());
diff --git a/src/mongo/db/query/find_and_modify_request.cpp b/src/mongo/db/query/find_and_modify_request.cpp
index 375ea84d7dc..2a5523c29e4 100644
--- a/src/mongo/db/query/find_and_modify_request.cpp
+++ b/src/mongo/db/query/find_and_modify_request.cpp
@@ -217,14 +217,9 @@ StatusWith<FindAndModifyRequest> FindAndModifyRequest::parseFromBSON(NamespaceSt
}
}
- if (update && update->type() == write_ops::UpdateModification::Type::kPipeline) {
- if (arrayFiltersSet) {
- return {ErrorCodes::FailedToParse, "Cannot specify arrayFilters and a pipeline update"};
- }
- if (!sort.isEmpty()) {
- // TODO SERVER-40405
- return {ErrorCodes::NotImplemented, "No support for sort and pipeline update"};
- }
+ if (update && update->type() == write_ops::UpdateModification::Type::kPipeline &&
+ arrayFiltersSet) {
+ return {ErrorCodes::FailedToParse, "Cannot specify arrayFilters and a pipeline update"};
}
FindAndModifyRequest request(std::move(fullNs), query, std::move(update));