Skip to content

Operations Group

RezaNajafian edited this page Dec 8, 2025 · 1 revision

Using Operations Groups

OperationsGroup<M> is the main way you interact with your models in MongoHelper. It groups CRUD and related operations for a specific model class M.

What is OperationsGroup?

Simplified definition:

public class OperationsGroup<M> {

    public MongoCollection<M> getCollection();

    public CountOperationsImpl<M> count();

    public CreateOperationsImpl<M> create();

    public DeleteOperationsImpl<M> delete();

    public FindOperationsImpl<M> find();

    public UpdateOperationsImpl<M> update();

    public UpsertOperationsImpl<M> upsert();
}

You do not construct OperationsGroup directly. Instead, once a schematic is registered, you call:

OperationsGroup<Post> posts = mongoHelper.get(Post.class);

Getting an OperationsGroup

MongoHelper mongoHelper = MongoHelper.create(client);

// after registering the schematic that contains Post
OperationsGroup<Post> posts = mongoHelper.get(Post.class);

If there is no OperationsGroup for a given class, an exception will be thrown indicating that no operations group is found for that class.

Example Usage Patterns

Note: The exact methods on CreateOperationsImpl, FindOperationsImpl, etc. depend on your implementation. The examples below show typical usage patterns.

Create

Post newPost = new Post();
// set fields...

posts.create()
     // e.g. .one(newPost) or .many(List.of(newPost1, newPost2))
     ;

Find

posts.find()
     // e.g. .byId(id) or .where(filter)
     ;

Update

posts.update()
     // e.g. .where(filter).set("title", "New Title")
     ;

Delete

posts.delete()
     // e.g. .where(filter).one() or .many()
     ;

Count

long count = posts.count()
        // e.g. .where(filter).execute()
        ;

Upsert

posts.upsert()
     // e.g. .filter(filter).set("field", value).execute()
     ;

Accessing the Underlying Collection

If you need to drop down to the raw MongoDB driver API, you can access the underlying MongoCollection<M>:

MongoCollection<Post> collection = posts.getCollection();

collection.insertOne(newPost); // direct driver usage

This gives you full flexibility while still benefiting from MongoHelper's metadata and grouped operations.

Clone this wiki locally