A lightweight Kotlin MongoDB ODM (Object Document Mapper) that provides a simple and type-safe way to interact with MongoDB databases.
- 🎯 Type-safe queries - Leverage Kotlin's type system for compile-time query validation
- 📝 Annotation-based mapping - Simple annotations for document and field mapping
- 🔍 Fluent query API - Intuitive query building with operators like
eq,gt,and,regex - 🚀 Easy repository pattern - Abstract base repository for common CRUD operations
- 🧪 Test-friendly - Built with testing in mind
- Kotlin 2.1.20+
- JDK 21+
- MongoDB instance (local or remote)
- Clone the repository
- Start MongoDB using Docker Compose:
docker-compose up -d- Run the tests to verify everything works:
./gradlew testUse annotations to map your Kotlin data classes to MongoDB documents:
@Document("users")
data class User(
@Id
val id: String,
@Field("first_name")
val name: String,
val age: Int
)Extend MongoRepository for your entities:
class UserRepository : MongoRepository<User>(User::class)// Connect to MongoDB
MongoConfig.connect("mongodb://localhost:27017", "your_database")
val userRepository = UserRepository()
// Insert a user
val user = User(id = UUID.randomUUID().toString(), name = "John", age = 30)
userRepository.insert(user)
// Find users with type-safe queries
val adults = userRepository.find(User::age gt 18)
val johnDoe = userRepository.findOne(User::name eq "John")
// Complex queries
val activeAdults = userRepository.find(
User::age gt 18 and (User::active eq true)
)eq- Equalsgt- Greater thanlt- Less thangte- Greater than or equallte- Less than or equalinF- In arrayregex- Regular expression matchand- Logical ANDor- Logical OR
@Document("collection_name")- Maps class to MongoDB collection@Id- Marks field as document ID@Field("field_name")- Maps property to different field name in MongoDB@Indexed- Creates index on field
The base MongoRepository provides:
insert(entity)- Insert single documentinsertMany(entities)- Insert multiple documentsfindById(id)- Find by IDfindOne(filter)- Find single document by filterfind(filter)- Find multiple documents by filterfindAll()- Find all documentsupsert(filter, entity)- Update or insert documentdeleteOne(filter)- Delete single document
src/
├── main/kotlin/
│ ├── annotations/ # Custom annotations
│ ├── mongo/ # MongoDB connection and utilities
│ └── repository/ # Repository base classes
└── test/kotlin/
├── repository/ # Repository implementations
└── schemas/ # Test data models
- MongoDB Java Driver 4.11.0
- Kotlinx Serialization 1.7.1
- Kotlin Reflection 2.0.0
./gradlew test./gradlew buildThis project is open source and available under the MIT License.