Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/mongo/db/catalog/index_key_validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,20 @@ StatusWith<BSONObj> validateIndexSpec(OperationContext* opCtx, const BSONObj& in

boost::optional<IndexVersion> resolvedIndexVersion;
std::string indexType;
std::set<StringData> fieldsNames;

for (auto&& indexSpecElem : indexSpec) {
auto indexSpecElemFieldName = indexSpecElem.fieldNameStringData();

if (fieldsNames.count(indexSpecElemFieldName)) {
return {ErrorCodes::BadValue,
str::stream() << "The field '" << indexSpecElemFieldName
<< "' appears more than one times in the index specification "
<< indexSpec
};
}
fieldsNames.insert(indexSpecElemFieldName);

if (IndexDescriptor::kKeyPatternFieldName == indexSpecElemFieldName) {
if (indexSpecElem.type() != BSONType::Object) {
return {ErrorCodes::TypeMismatch,
Expand Down
5 changes: 5 additions & 0 deletions src/mongo/db/catalog/index_key_validate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,5 +526,10 @@ TEST(IndexKeyValidateTest, GeoIndexSpecs) {
fromjson("{'key':{'loc':'2dsphere'},'name':'loc_2dsphere','finestIndexedLevel':true,'"
"coarsestIndexedLevel':5}")));
}

TEST(IndexKeyValidateTest, DuplicatedFieldNames) {
ASSERT_NOT_OK(index_key_validate::validateIndexSpec(
nullptr, fromjson("{key: {a: 1}, name: 'index', background: true, background: true}")));
}
} // namespace
} // namespace mongo