From 2c7681d8c20af6ba8ffc97fe63337ce33a6eb435 Mon Sep 17 00:00:00 2001 From: James Wahlin Date: Tue, 7 May 2019 12:30:07 -0400 Subject: SERVER-40405 Add support for 'sort' option in combination with a pipeline-style findAndModify --- jstests/core/find_and_modify_pipeline_update.js | 19 ++++++++++++++----- src/mongo/db/query/find_and_modify_request.cpp | 11 +++-------- 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::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)); -- cgit v1.2.1