diff options
author | Cheahuychou Mao <cheahuychou.mao@mongodb.com> | 2020-04-09 03:03:41 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-14 15:05:47 +0000 |
commit | 20364840b8f1af16917e4c23c1b5f5efd8b352f8 (patch) | |
tree | d24fd0e190c78831d27718c19988db7cfd2596a7 /src/mongo/db | |
parent | fafc69977611a6ee306f3e8dd2b8bad065556d29 (diff) | |
download | mongo-r4.2.6-rc0.tar.gz |
SERVER-47436 Make shards validate shardKey in dataSize commandr4.2.6-rc0r4.2.6
(cherry picked from commit 59e005fea0e1ca575083ded8c02c518048fb8af0)
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/commands/dbcommands.cpp | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp index 24d2b4aaea7..63e72e2a3f0 100644 --- a/src/mongo/db/commands/dbcommands.cpp +++ b/src/mongo/db/commands/dbcommands.cpp @@ -455,9 +455,31 @@ public: BSONObj keyPattern = jsobj.getObjectField("keyPattern"); bool estimate = jsobj["estimate"].trueValue(); - AutoGetCollectionForReadCommand ctx(opCtx, NamespaceString(ns)); - + const NamespaceString nss(ns); + AutoGetCollectionForReadCommand ctx(opCtx, nss); Collection* collection = ctx.getCollection(); + + const auto metadata = CollectionShardingState::get(opCtx, nss)->getCurrentMetadata(); + + if (metadata->isSharded()) { + const ShardKeyPattern shardKeyPattern(metadata->getKeyPattern()); + uassert(ErrorCodes::BadValue, + "keyPattern must be empty or must be an object that equals the shard key", + keyPattern.isEmpty() || + (SimpleBSONObjComparator::kInstance.evaluate(shardKeyPattern.toBSON() == + keyPattern))); + + uassert(ErrorCodes::BadValue, + str::stream() << "min value " << min << " does not have shard key", + min.isEmpty() || shardKeyPattern.isShardKey(min)); + min = shardKeyPattern.normalizeShardKey(min); + + uassert(ErrorCodes::BadValue, + str::stream() << "max value " << max << " does not have shard key", + max.isEmpty() || shardKeyPattern.isShardKey(max)); + max = shardKeyPattern.normalizeShardKey(max); + } + long long numRecords = 0; if (collection) { numRecords = collection->numRecords(opCtx); |