Skip to content

Commit

Permalink
Add MagicCollection#insertOneReturnId(T)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Sep 29, 2024
1 parent 0019c13 commit 01894b1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/xyz/srnyx/magicmongo/MagicCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -85,6 +86,18 @@ public List<T> findMany(@NotNull Bson filter) {
return find(filter).into(new ArrayList<>());
}

/**
* Inserts a document in the collection and returns the inserted document's ID
*
* @param t the document to insert
*
* @return the ID of the inserted document
*/
@NotNull
public ObjectId insertOneReturnId(@NotNull T t) {
return Objects.requireNonNull(insertOne(t).getInsertedId()).asObjectId().getValue();
}

/**
* Upserts a document in the collection
*
Expand Down

0 comments on commit 01894b1

Please sign in to comment.