From 4cb4ceb95fa4164f199ed8b00f5e691f361e1a7b Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Thu, 19 Oct 2023 18:33:52 -0400 Subject: [PATCH] Added `LazyCollection#upsertOne(...)` --- .../xyz/srnyx/lazylibrary/LazyCollection.java | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java b/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java index c437668..ea72e63 100644 --- a/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java +++ b/library/src/main/java/xyz/srnyx/lazylibrary/LazyCollection.java @@ -5,6 +5,7 @@ import com.mongodb.client.model.Filters; import com.mongodb.client.model.FindOneAndUpdateOptions; import com.mongodb.client.model.ReturnDocument; +import com.mongodb.client.model.UpdateOptions; import org.bson.conversions.Bson; @@ -84,6 +85,16 @@ public void updateOne(@NotNull Bson filter, @NotNull Bson update) { collection.updateOne(filter, update); } + /** + * Upserts a document in the collection + * + * @param filter the filter to apply + * @param update the update to apply + */ + public void upsertOne(@NotNull Bson filter, @NotNull Bson update) { + collection.updateOne(filter, update, new UpdateOptions().upsert(true)); + } + /** * Updates a document in the collection and returns the updated document * @@ -94,7 +105,8 @@ public void updateOne(@NotNull Bson filter, @NotNull Bson update) { */ @Nullable public T findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { - return collection.findOneAndUpdate(filter, update, getReturnAfter()); + return collection.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() + .returnDocument(ReturnDocument.AFTER)); } /** @@ -107,7 +119,9 @@ public T findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { */ @Nullable public T findOneAndUpsert(@NotNull Bson filter, @NotNull Bson update) { - return collection.findOneAndUpdate(filter, update, getUpsert()); + return collection.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions() + .returnDocument(ReturnDocument.AFTER) + .upsert(true)); } /** @@ -128,24 +142,4 @@ public void deleteOne(@NotNull Bson filter) { public void deleteOne(@NotNull String field, @Nullable Object value) { deleteOne(Filters.eq(field, value)); } - - /** - * Returns a new {@link FindOneAndUpdateOptions} instance with the {@link ReturnDocument#AFTER} option set - * - * @return the {@link FindOneAndUpdateOptions} instance - */ - @NotNull - private static FindOneAndUpdateOptions getReturnAfter() { - return new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER); - } - - /** - * Returns a new {@link FindOneAndUpdateOptions} instance with the {@link ReturnDocument#AFTER} and {@link FindOneAndUpdateOptions#upsert(boolean)} options set - * - * @return the {@link FindOneAndUpdateOptions} instance - */ - @NotNull - private static FindOneAndUpdateOptions getUpsert() { - return getReturnAfter().upsert(true); - } }