summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/dbcommands.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/commands/dbcommands.cpp')
-rw-r--r--src/mongo/db/commands/dbcommands.cpp26
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);