diff --git a/src/main/java/xyz/refinedev/api/storage/MongoStorage.java b/src/main/java/xyz/refinedev/api/storage/MongoStorage.java index 669b522..5a2199f 100644 --- a/src/main/java/xyz/refinedev/api/storage/MongoStorage.java +++ b/src/main/java/xyz/refinedev/api/storage/MongoStorage.java @@ -7,6 +7,7 @@ import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOptions; +import com.mongodb.client.model.Updates; import org.bson.Document; import org.bson.conversions.Bson; @@ -32,10 +33,12 @@ public class MongoStorage { private final MongoCollection collection; private final Gson gson; + private final Type typeToken; public MongoStorage(MongoCollection collection, Gson gson) { this.collection = collection; this.gson = gson; + this.typeToken = new TypeToken() {}.getType(); } public CompletableFuture> fetchAllEntries() { @@ -45,7 +48,6 @@ public CompletableFuture> fetchAllEntries() { if (document == null) { continue; } - Type typeToken = new TypeToken() {}.getType(); found.add(this.gson.fromJson(document.toJson(), typeToken)); } return found; @@ -117,19 +119,13 @@ public void deleteData(UUID key) { * @param key {@link String key} * @return {@link Integer amount of deleted documents} */ - public CompletableFuture deleteKeyInAll(String key) { + public CompletableFuture deleteKeyInAll(String key) { return CompletableFuture.supplyAsync(() -> { - int deleteCount = 0; - for ( Document document : collection.find() ) { - if (document == null) continue; + // Unset the key + Bson combinedUpdate = Updates.unset(key); - if (document.get(key) != null) { - document.remove(key); - deleteCount++; - } - collection.replaceOne(Filters.eq("_id", document.get("_id")), document); - } - return deleteCount; + // Apply the updates to all documents in the collection + return collection.updateMany(new Document(), combinedUpdate).getModifiedCount(); // new Document() is an empty filter, meaning "all documents"; }); } } diff --git a/src/main/java/xyz/refinedev/api/storage/YamlStorage.java b/src/main/java/xyz/refinedev/api/storage/YamlStorage.java index 99fa706..ccd8b70 100644 --- a/src/main/java/xyz/refinedev/api/storage/YamlStorage.java +++ b/src/main/java/xyz/refinedev/api/storage/YamlStorage.java @@ -1,6 +1,5 @@ package xyz.refinedev.api.storage; -import org.apache.commons.io.Charsets; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;