From 66ed8cbe8277e63d5c908b68ba8721af23390d1a Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Thu, 8 Nov 2018 15:51:21 -0500 Subject: SERVER-37727 dropIndexes supports dropping multiple indexes --- src/mongo/db/catalog/drop_indexes.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/mongo/db/catalog/drop_indexes.cpp') diff --git a/src/mongo/db/catalog/drop_indexes.cpp b/src/mongo/db/catalog/drop_indexes.cpp index 6b976b83892..2c25819720b 100644 --- a/src/mongo/db/catalog/drop_indexes.cpp +++ b/src/mongo/db/catalog/drop_indexes.cpp @@ -54,6 +54,7 @@ namespace { // 1) '*' - drop all indexes. // 2) - name of single index to drop. // 3) - BSON document representing key pattern of index to drop. +// 4) [, , ...] - array containing names of indexes to drop. constexpr auto kIndexFieldName = "index"_sd; /** @@ -159,6 +160,35 @@ Status wrappedRun(OperationContext* opCtx, return Status::OK(); } + // The 'index' field contains a list of names of indexes to drop. + // Drops all or none of the indexes due to the enclosing WriteUnitOfWork. + if (indexElem.type() == Array) { + for (auto indexNameElem : indexElem.Array()) { + if (indexNameElem.type() != String) { + return Status(ErrorCodes::TypeMismatch, + str::stream() << "dropIndexes " << collection->ns().ns() << " (" + << collection->uuid() + << ") failed to drop multiple indexes " + << indexElem.toString(false) + << ": index name must be a string"); + } + + auto indexToDelete = indexNameElem.String(); + auto status = dropIndexByName(opCtx, collection, indexCatalog, indexToDelete); + if (!status.isOK()) { + return status.withContext(str::stream() << "dropIndexes " << collection->ns().ns() + << " (" + << collection->uuid() + << ") failed to drop multiple indexes " + << indexElem.toString(false) + << ": " + << indexToDelete); + } + } + + return Status::OK(); + } + return Status(ErrorCodes::IndexNotFound, str::stream() << "invalid index name spec: " << indexElem.toString(false)); } -- cgit v1.2.1