MongoHelper is a small ORM-like helper library built on top of the official MongoDB Java driver. It provides:
- Central registration of models via
OrmSchematic - Metadata-driven collections and indexes
- Grouped CRUD operations per model via
OperationsGroup
The goal is to keep your MongoDB access code simple, consistent, and metadata-driven, while still letting you drop down to the raw MongoDB driver whenever you need.
- Define your schema in one place with
OrmSchematic - Annotate models and fields using
@OrmModel,@OrmField, and index annotations - Automatic creation of collections and indexes based on model metadata
- Strongly-typed grouped operations for each model (
count,create,delete,find,update,upsert) - Pluggable codec registries for custom value types and enums
- Java 17+ (adjust if your project targets another version)
- MongoDB server (local or remote)
- MongoDB Java driver (sync)
Add MongoHelper and the MongoDB Java driver to your build.
<dependencies>
<!-- MongoDB Java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>VERSION_HERE</version>
</dependency>
<!-- MongoHelper -->
<dependency>
<groupId>net.clydo</groupId>
<artifactId>mongohelper</artifactId>
<version>VERSION_HERE</version>
</dependency>
</dependencies>dependencies {
implementation("org.mongodb:mongodb-driver-sync:VERSION_HERE")
implementation("net.clydo:mongohelper:VERSION_HERE")
}Replace VERSION_HERE with the actual versions you use or publish.
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
MongoClient client = MongoClients.create("mongodb://localhost:27017");import net.clydo.mongo.MongoHelper;
MongoHelper helper = MongoHelper.create(client);import net.clydo.mongo.OrmSchematic;
import org.bson.codecs.configuration.CodecRegistry;
import java.util.List;
public class UserSchematic implements OrmSchematic {
@Override
public String getDatabaseName() {
return "app_db";
}
@Override
public List<Class<?>> getModelClasses() {
return List.of(User.class);
}
@Override
public List<Class<?>> getTypeClasses() {
return List.of();
}
@Override
public List<Class<? extends Enum<?>>> getEnumClasses() {
return List.of();
}
@Override
public List<CodecRegistry> handleCodecRegistry(List<CodecRegistry> registries) {
return registries;
}
}import net.clydo.mongo.annotations.OrmModel;
import net.clydo.mongo.annotations.OrmField;
import org.bson.types.ObjectId;
@OrmModel("users")
public class User {
@OrmField("_id")
private ObjectId id;
@OrmField("email")
private String email;
@OrmField("age")
private int age;
public User() {
}
// getters/setters ...
}import net.clydo.mongo.operations.OperationsGroup;
helper.register(UserSchematic.class);
OperationsGroup<User> users = helper.get(User.class);
// Example usage (pattern only; adjust to your operations API):
User newUser = new User();
newUser.setEmail("john@example.com");
newUser.setAge(30);
users.create()
// .one(newUser)
;
users.find()
// .where(eq("email", "john@example.com"))
;The exact methods on create(), find(), etc. depend on the concrete operation implementations in this project; see the wiki for details.
This project is licensed under the GNU General Public License v3.0 (GPL-3.0), as stated in the source file headers. See the license text at: