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
Original file line number Diff line number Diff line change
Expand Up @@ -427,25 +427,34 @@ private void dropIndexes(MongoCollection<P> collection, Document query) {
Object index = query.get("index");
Assert.notNull(index, () -> "Index name must not be null");
MongoCollection<P> indexCollection = indexes.get();
Document nsQuery = new Document("ns", collection.getFullName());
if (Objects.equals(index, "*")) {
for (Document indexDocument : indexCollection.queryAll()) {
for (Document indexDocument : indexCollection.handleQuery(nsQuery)) {
Document indexKeys = (Document) indexDocument.get("key");
if (!isPrimaryKeyIndex(indexKeys)) {
dropIndex(collection, indexDocument);
}
}
} else if (index instanceof String) {
dropIndex(collection, new Document("name", index));
} else {
Document indexKeys = (Document) index;
Document indexQuery = new Document("key", indexKeys).append("ns", collection.getFullName());
Document indexToDrop = CollectionUtils.getSingleElement(indexCollection.handleQuery(indexQuery),
() -> new IndexNotFoundException(indexKeys));
if (index instanceof String indexName) {
nsQuery.append("name", indexName);
} else {
nsQuery.append("key", (Document) index);
}
Document indexToDrop = CollectionUtils.getSingleElement(indexCollection.handleQuery(nsQuery),
() -> createIndexNotFoundException(index));
int numDeleted = dropIndex(collection, indexToDrop);
Assert.equals(numDeleted, 1, () -> "Expected one deleted document");
}
}

private static IndexNotFoundException createIndexNotFoundException(Object index) {
if (index instanceof String indexName) {
return new IndexNotFoundException("index not found with name [" + indexName + "]");
}
return new IndexNotFoundException((Document) index);
}

private int dropIndex(MongoCollection<P> collection, Document indexDescription) {
String indexName = (String) indexDescription.get("name");
dropIndex(collection, indexName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,82 @@ void testDropIndexes_twoIndexesWithTheSameKey() {
);
}

// https://github.com/bwaldvogel/mongo-java-server/issues/246
@Test
void testDropIndexes_string_twoIndexesWithTheSameKey() {
collection.insertOne(json("_id: 1, c: 10"));

MongoCollection<Document> otherCollection = getCollection("other");
otherCollection.insertOne(json("_id: 1, c: 10"));

String indexName = collection.createIndex(new Document("c", 1));
otherCollection.createIndex(new Document("c", 1));

assertThat(collection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2)
);

assertThat(otherCollection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2)
);

collection.dropIndex(indexName);

assertThat(collection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2)
);

assertThat(otherCollection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2)
);
}

// https://github.com/bwaldvogel/mongo-java-server/issues/247
@Test
void testDropIndexes_all() {
collection.insertOne(json("_id: 1, c: 10, d:1"));

MongoCollection<Document> otherCollection = getCollection("other");
otherCollection.insertOne(json("_id: 1, c: 10"));

collection.createIndex(new Document("c", 1));
collection.createIndex(new Document("d", 1));
otherCollection.createIndex(new Document("c", 1));

assertThat(collection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2),
json("key: {d: 1}").append("name", "d_1").append("v", 2)
);

assertThat(otherCollection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2)
);

collection.dropIndex("*");

assertThat(collection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2)
);

assertThat(otherCollection.listIndexes())
.containsExactlyInAnyOrder(
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
json("key: {c: 1}").append("name", "c_1").append("v", 2)
);
}

@Test
public void testCurrentOperations() {
Document currentOperations = getAdminDb().getCollection("$cmd.sys.inprog").find().first();
Expand Down