Skip to content

Commit 28c937f

Browse files
committed
Fix for issue #247
1 parent f5fcdfa commit 28c937f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoDatabase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ private void dropIndexes(MongoCollection<P> collection, Document query) {
428428
Assert.notNull(index, () -> "Index name must not be null");
429429
MongoCollection<P> indexCollection = indexes.get();
430430
if (Objects.equals(index, "*")) {
431-
for (Document indexDocument : indexCollection.queryAll()) {
431+
Document nsQuery = new Document("ns", collection.getFullName());
432+
for (Document indexDocument : indexCollection.handleQuery(nsQuery)) {
432433
Document indexKeys = (Document) indexDocument.get("key");
433434
if (!isPrimaryKeyIndex(indexKeys)) {
434435
dropIndex(collection, indexDocument);

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ void testDropIndexes_twoIndexesWithTheSameKey() {
723723
);
724724
}
725725

726+
// https://github.com/bwaldvogel/mongo-java-server/issues/246
726727
@Test
727728
void testDropIndexes_string_twoIndexesWithTheSameKey() {
728729
collection.insertOne(json("_id: 1, c: 10"));
@@ -759,6 +760,45 @@ void testDropIndexes_string_twoIndexesWithTheSameKey() {
759760
);
760761
}
761762

763+
// https://github.com/bwaldvogel/mongo-java-server/issues/247
764+
@Test
765+
void testDropIndexes_all() {
766+
collection.insertOne(json("_id: 1, c: 10, d:1"));
767+
768+
MongoCollection<Document> otherCollection = getCollection("other");
769+
otherCollection.insertOne(json("_id: 1, c: 10"));
770+
771+
collection.createIndex(new Document("c", 1));
772+
collection.createIndex(new Document("d", 1));
773+
otherCollection.createIndex(new Document("c", 1));
774+
775+
assertThat(collection.listIndexes())
776+
.containsExactlyInAnyOrder(
777+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
778+
json("key: {c: 1}").append("name", "c_1").append("v", 2),
779+
json("key: {d: 1}").append("name", "d_1").append("v", 2)
780+
);
781+
782+
assertThat(otherCollection.listIndexes())
783+
.containsExactlyInAnyOrder(
784+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
785+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
786+
);
787+
788+
collection.dropIndex("*");
789+
790+
assertThat(collection.listIndexes())
791+
.containsExactlyInAnyOrder(
792+
json("key: {_id: 1}").append("name", "_id_").append("v", 2)
793+
);
794+
795+
assertThat(otherCollection.listIndexes())
796+
.containsExactlyInAnyOrder(
797+
json("key: {_id: 1}").append("name", "_id_").append("v", 2),
798+
json("key: {c: 1}").append("name", "c_1").append("v", 2)
799+
);
800+
}
801+
762802
@Test
763803
public void testCurrentOperations() {
764804
Document currentOperations = getAdminDb().getCollection("$cmd.sys.inprog").find().first();

0 commit comments

Comments
 (0)