summaryrefslogtreecommitdiff
path: root/src/mongo/db/exec
diff options
context:
space:
mode:
authorJack Mulrow <jack.mulrow@mongodb.com>2020-03-02 09:38:33 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-03-17 14:54:45 +0000
commitd98886efd15f146dad8c217db90e8cb4b2c0e851 (patch)
tree676072f3454c0ba170b80d9873b4678fa05c6ebc /src/mongo/db/exec
parent64adc0476a748ef54b8cb4ed6e086da970f22f82 (diff)
downloadmongo-d98886efd15f146dad8c217db90e8cb4b2c0e851.tar.gz
SERVER-46505 Restore requirement that full shard key must be in query to update shard key value
Diffstat (limited to 'src/mongo/db/exec')
-rw-r--r--src/mongo/db/exec/update_stage.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/mongo/db/exec/update_stage.cpp b/src/mongo/db/exec/update_stage.cpp
index 8e237d622f1..723a64e7749 100644
--- a/src/mongo/db/exec/update_stage.cpp
+++ b/src/mongo/db/exec/update_stage.cpp
@@ -719,6 +719,22 @@ bool UpdateStage::checkUpdateChangesShardKeyFields(ScopedCollectionDescription c
// Assert that the updated doc has no arrays or array descendants for the shard key fields.
_assertPathsNotArray(_doc, shardKeyPaths);
+ // We do not allow modifying shard key value without specifying the full shard key in the query.
+ // If the query is a simple equality match on _id, then '_params.canonicalQuery' will be null.
+ // But if we are here, we already know that the shard key is not _id, since we have an assertion
+ // earlier for requests that try to modify the immutable _id field. So it is safe to uassert if
+ // '_params.canonicalQuery' is null OR if the query does not include equality matches on all
+ // shard key fields.
+ const auto& shardKeyPathsVector = collDesc->getKeyPatternFields();
+ pathsupport::EqualityMatches equalities;
+ uassert(31025,
+ "Shard key update is not allowed without specifying the full shard key in the query",
+ _params.canonicalQuery &&
+ pathsupport::extractFullEqualityMatches(
+ *(_params.canonicalQuery->root()), shardKeyPaths, &equalities)
+ .isOK() &&
+ equalities.size() == shardKeyPathsVector.size());
+
// We do not allow updates to the shard key when 'multi' is true.
uassert(ErrorCodes::InvalidOptions,
"Multi-update operations are not allowed when updating the shard key field.",