{"id":46553892,"url":"https://github.com/inflearn/spring-data-mongodb-kotlin-dsl","last_synced_at":"2026-03-07T04:01:29.671Z","repository":{"id":194941458,"uuid":"686606328","full_name":"inflearn/spring-data-mongodb-kotlin-dsl","owner":"inflearn","description":"Kotlin library that provides a type-safe DSL for aggregations in Spring Data MongoDB.","archived":false,"fork":false,"pushed_at":"2025-11-25T05:09:52.000Z","size":919,"stargazers_count":20,"open_issues_count":9,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-11-28T13:54:22.770Z","etag":null,"topics":["atlas-search","dsl","hacktoberfest","kotlin","mongodb","spring-boot","spring-data-mongodb"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inflearn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-09-03T11:12:04.000Z","updated_at":"2025-11-25T05:09:28.000Z","dependencies_parsed_at":"2025-11-28T00:10:28.231Z","dependency_job_id":null,"html_url":"https://github.com/inflearn/spring-data-mongodb-kotlin-dsl","commit_stats":{"total_commits":311,"total_committers":5,"mean_commits":62.2,"dds":"0.40192926045016075","last_synced_commit":"d0a2feb4c518d61003bfd3fcde2f13825ea1e4d5"},"previous_names":["inflearn/spring-data-mongodb-kotlin-dsl"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/inflearn/spring-data-mongodb-kotlin-dsl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inflearn%2Fspring-data-mongodb-kotlin-dsl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inflearn%2Fspring-data-mongodb-kotlin-dsl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inflearn%2Fspring-data-mongodb-kotlin-dsl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inflearn%2Fspring-data-mongodb-kotlin-dsl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inflearn","download_url":"https://codeload.github.com/inflearn/spring-data-mongodb-kotlin-dsl/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inflearn%2Fspring-data-mongodb-kotlin-dsl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30207390,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T03:24:23.086Z","status":"ssl_error","status_checked_at":"2026-03-07T03:23:11.444Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atlas-search","dsl","hacktoberfest","kotlin","mongodb","spring-boot","spring-data-mongodb"],"created_at":"2026-03-07T04:01:27.579Z","updated_at":"2026-03-07T04:01:29.641Z","avatar_url":"https://github.com/inflearn.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring-data-mongodb-kotlin-dsl\n\nThis project is a Kotlin library that provides a type-safe DSL for aggregations in Spring Data MongoDB.\n\n## Key Features\n\n- Type-safe DSL for building pipeline of [aggregation operations](https://www.mongodb.com/docs/manual/aggregation/)\n- Support for [Atlas Search](https://docs.atlas.mongodb.com/atlas-search)\n- Provide Kotlin Doc (KDoc) for all DSL functions and properties\n\n## Requirements\n\n- Spring Data MongoDB 3.4.0 or higher\n- Java 8 or higher\n- Kotlin 1.8 or higher\n\n## Installation\n\n\u003e [!NOTE]  \n\u003e We do not have a publishing mechanism to a maven repository so the easiest way to add the library to your app is via a JitPack Dependency.\n\n[![](https://jitpack.io/v/inflearn/spring-data-mongodb-kotlin-dsl.svg)](https://jitpack.io/#inflearn/spring-data-mongodb-kotlin-dsl)\n\n- build.gradle.kts\n\n```kotlin\nrepositories {\n    maven { setUrl(\"https://jitpack.io\") }\n}\n\ndependencies {\n    implementation(\"com.github.inflearn:spring-data-mongodb-kotlin-dsl:$version\")\n}\n```\n\n## Why do you need this library?\n\n### Type-safe DSL for aggregation operations\n\nSpring Data MongoDB provides a convenient way to create [aggregation operations](https://www.mongodb.com/docs/manual/aggregation/).\nWith this library, you can use aggregation operations in a more concise and type-safe way.\n\nFor example, if you want to use the `$subtract` operator in the `$project` stage, \nyou can write the following code using Spring Data MongoDB.\n\n```java\nAggregation.newAggregation(\n    Aggregation.project()\n        .andInclude(\"item\")\n        .and(ArithmeticOperators.Subtract.valueOf(\"date\").subtract(5 * 60 * 1000))\n        .`as`(\"dateDifference\"),\n)\n```\n\nyou can write the same code as follows.\n\n```kotlin\naggregation {\n    project {\n        +Sales::item\n        \"dateDifference\" expression {\n            subtract {\n                of(Sales::date) - (5 * 60 * 1000)\n            }\n        }\n    }\n}\n```\n\n### DSL for Atlas Search\n\nSpring Data MongoDB does not provide a convenient way to use [Atlas Search](https://docs.atlas.mongodb.com/atlas-search).\nIn [official documentation](https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation.supported-aggregation-operations),\nit suggests using `stage` method and passing the aggregation pipeline as a string.\n\n```java\nAggregation.stage(\"\"\"\n    { $search : {\n        \"near\": {\n          \"path\": \"released\",\n          \"origin\": { \"$date\": { \"$numberLong\": \"...\" } } ,\n          \"pivot\": 7\n        }\n      }\n    }\n\"\"\");\n```\n\nThis approach has the following disadvantages.\n\n- It is not type-safe.\n- It is not easy to read and maintain.\n- It is easy to make a mistake in the query.\n\nHowever, if you use this library, you can use Atlas Search in a type-safe way.\n\n```kotlin\naggregation {\n    search {\n        near {\n            path(Movies::released)\n            origin(LocalDateTime.now())\n            pivot(7)\n        }\n    }\n}\n```\n\n### KDoc for all DSL functions and properties\n\nThis library provides KDoc for all DSL functions and properties.\nYou can easily find out what each function and property does by hovering over it in your IDE.\n\n![kdoc.png](image/kdoc.png)\n\n## Examples\n\nThis project provides `example` module that contains examples of how to use the library.\nThe codes used in the example are all DSL representations of the examples from the official MongoDB documentation.\nEach aggregation operation is implemented as a separate repository class with `MongoTemplate` as a dependency.\n\nFor example, There is an example of `text` operation from `$search` stage\nin [TextSearchRepository.kt](example/spring-data-mongodb/src/main/kotlin/com/github/inflab/example/spring/data/mongodb/repository/atlas/TextSearchRepository.kt)\n\n```kotlin\n@Repository\nclass TextSearchRepository(\n    private val mongoTemplate: MongoTemplate,\n) {\n\n    data class TitleAndScoreDto(\n        val title: String,\n        val score: Double,\n    )\n\n    /**\n     * @see \u003ca href=\"https://www.mongodb.com/docs/atlas/atlas-search/text/#basic-example\"\u003eBasic Example\u003c/a\u003e\n     */\n    fun findTitleWithSufer(): AggregationResults\u003cTitleAndScoreDto\u003e {\n        val aggregation = aggregation {\n            search {\n                text {\n                    path(Movies::title)\n                    query(\"surfer\")\n                }\n            }\n\n            project {\n                excludeId()\n                +Movies::title\n                searchScore()\n            }\n        }\n\n        return mongoTemplate.aggregate\u003cMovies, TitleAndScoreDto\u003e(aggregation)\n    }\n}\n```\n\nIt is equivalent to the following MongoDB query\nfrom [the official documentation](https://www.mongodb.com/docs/atlas/atlas-search/text/#basic-example).\n\n```javascript\ndb.movies.aggregate([\n    {\n        $search: {\n            \"text\": {\n                \"path\": \"title\",\n                \"query\": \"surfer\"\n            }\n        }\n    },\n    {\n        $project: {\n            \"_id\": 0,\n            \"title\": 1,\n            score: {$meta: \"searchScore\"}\n        }\n    }\n])\n```\n\n### Running tests from examples\n\nEach example repository contains a test code that verifies that the query is executed correctly.\nThere are two types of tests: one that requires a MongoDB Community instance and another that requires MongoDB Atlas.\n\n- MongoDB Community\n\nThe repositories that are **not under** the `com.github.inflab.example.spring.data.mongodb.repository.atlas` package requires a MongoDB Community instance.\nTo run the MongoDB Community instance, we use [Testcontainers](https://www.testcontainers.org/).\nTherefore, you need to install and run Docker on your machine.\n\n- MongoDB Atlas\n\nThe repositories that are under the `com.github.inflab.example.spring.data.mongodb.repository.atlas` package requires a MongoDB Atlas instance.\nYou need to [create a MongoDB Atlas instance](https://www.mongodb.com/docs/atlas/getting-started) and set the connection information in the `application.yml` file.\n\n\u003e [!NOTE]\n\u003e you can create the `application.yml` file by copying `example/spring-data-mongodb/src/test/resources/application.sample.yml` file.\n\n```yaml\nspring:\n  data:\n    mongodb:\n      username: \"username\"\n      password: \"password\"\n      host: \"host\"\n```\n\nYou should refer to [the following manual](https://www.mongodb.com/docs/atlas/sample-data/) to configure sample data as well.\nBecause most example codes are provided based on the sample data.\nIf test codes are using `Atlas Search`, you also need to create a suitable search index.\nPlease refer to each documentation of the example code for more information.\n\n## Contributors\n\n\u003ca href=\"https://github.com/inflearn/spring-data-mongodb-kotlin-dsl/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=inflearn/spring-data-mongodb-kotlin-dsl\" /\u003e\n\u003c/a\u003e\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finflearn%2Fspring-data-mongodb-kotlin-dsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finflearn%2Fspring-data-mongodb-kotlin-dsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finflearn%2Fspring-data-mongodb-kotlin-dsl/lists"}