{"id":13806122,"url":"https://github.com/eeb/dropwizard-mongo","last_synced_at":"2025-05-13T21:32:21.136Z","repository":{"id":18174325,"uuid":"21286307","full_name":"eeb/dropwizard-mongo","owner":"eeb","description":"Factories and health checks for connecting to mongoDB from dropwizard.","archived":false,"fork":false,"pushed_at":"2015-05-17T19:47:58.000Z","size":11587,"stargazers_count":20,"open_issues_count":0,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-04T01:05:55.331Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eeb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-27T19:07:49.000Z","updated_at":"2021-02-23T19:25:19.000Z","dependencies_parsed_at":"2022-09-24T22:33:24.928Z","dependency_job_id":null,"html_url":"https://github.com/eeb/dropwizard-mongo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eeb%2Fdropwizard-mongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eeb%2Fdropwizard-mongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eeb%2Fdropwizard-mongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eeb%2Fdropwizard-mongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eeb","download_url":"https://codeload.github.com/eeb/dropwizard-mongo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225260373,"owners_count":17446086,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-08-04T01:01:08.071Z","updated_at":"2024-11-18T22:31:19.452Z","avatar_url":"https://github.com/eeb.png","language":"Java","readme":"#Connect dropwizard to MongoDB \n\n\n*dropwizard-mongo is a set of factories and health checks to be used with [dropwizard](http://dropwizard.github.io/dropwizard/) for connecting to MongoDB.*\n\n## Disclaimer\nUpdate 8/1: Major refactor that simplifies usage. Thanks to [kgilmer](https://github.com/kgilmer) for the review.  \nThis project is brand new, so I have a lot of additional work to support the multiple options that can be passed to the MongoClient, DB, and DBCollection objects.\n\n##Usage\n\n### Installation\n\nThe project artifacts are not currently hosted in a maven repository, so you will need to clone the repo and build the dropwizard-mongo project at a minimum.\n\nI recommend you use dropwizard-mongo in combination with the [MongoJack](http://mongojack.org/) project for quickest implementation. The dropwizard-mongo-example project shows an example of using MongoJack with dropwizard-mongo.\n\n### Updating Your yaml\n\nIf you want the factory to build a [MongoClient](https://api.mongodb.org/java/current/com/mongodb/MongoClient.html) object you can specify the connections in you config file.\n\n    mongoDB:\n      connections:\n        - host: localhost\n          port: 27017\n        - host: 192.168.1.12\n          port: 27017\n              \nIf you want to the factory to build a [DB](https://api.mongodb.org/java/current/com/mongodb/DB.html) object you will need to further specify the db name in the config.\n\n    mongoDB:\n        dbName: unittest\n        connections:\n            - host: localhost\n              port: 27017\n            - host: 192.168.1.12\n              port: 27017\n\nFinally, the factory can return a [DBCollection](https://api.mongodb.org/java/current/com/mongodb/DBCollection.html) object\n you will need to further specify the collection name in the config.\n\n    mongoDB:\n        dbName: unittest\n        collName: test\n        connections:\n            - host: localhost\n              port: 27017\n            - host: 192.168.1.12\n              port: 27017\n                \n      \n### Updating you configuration\nYou will need to add a reference to MongoFactory in your class that extends Configuration or \nyou can extend from com.eeb.dropwizardmongo.configuration.DropwizardMongoConfiguration.\n\n    @Valid\n    @NotNull\n    private MongoFactory mongoFactory = new MongoFactory();\n      \n    @JsonProperty(\"mongoDB\")\n    public MongoFactory getMongoFactory() {\n        return this.mongoFactory;\n    }\n      \n    @JsonProperty(\"mongoDB\")\n    public void setMongoFactory(MongoFactory MongoFactory) {\n        this.mongoFactory = MongoFactory;\n    }         \n      \n      \n\n### Update your applications run method\nYour class that extends from Application needs to be updated to register the health checks and call the \nfactory method you wish to use. Once you have the object you want you can pass it to your dropwizard resources.\n\n#### Registering the health checks\n     final MongoClient mongoClient = config.getMongoFactory().buildClient(environment);\n     environment.healthChecks().register(\"mongo\",new MongoHealthCheck(mongoClient));\n\n#### Building a MongoClient object\n     final MongoClient mongoClient = config.getMongoFactory().buildClient(environment); \n     //Register Resources\n     environment.jersey().register(new CollectionIdsResource(mongoClient));\n      \n#### Building a DB object\n     final DB db = config.getMongoFactory().buildDB(environment); \n     //Register Resources\n     environment.jersey().register(new CollectionIdsResource(db));\n    \n#### Building a DBCollection object    \n    final DBCollection coll = config.getMongoFactory().buildColl(environment); \n    //Register Resources\n    environment.jersey().register(new CollectionIdsResource(coll));\n         \n### Using MongoJack with dropwizard-mongo\n\nThe secret sauce here is MongoJack. It lets me pass my Jackson API objects directly to the MongoDB API as well as returning the results of queries as my API objects.\n\nSuper basic API object representing a MongoDB document:\n\n    public class MongoDocument {\n\n        private String id;\n\n        @ObjectId\n        @JsonProperty(\"_id\")\n        public String getId() {\n            return this.id;\n        }\n    \n        @ObjectId\n        @JsonProperty(\"_id\")\n        public void setId(String id) {\n            this.id = id;\n        }\n\n    }\n\nMy Resource's GET handler.\n\n    @GET\n    public List\u003cMongoDocument\u003e fetch(@PathParam(\"collection\") String collection) {\n       \n        JacksonDBCollection\u003cMongoDocument, String\u003e coll = JacksonDBCollection.wrap(mongoDB.getCollection(collection), MongoDocument.class,\n                String.class);\n        DBCursor\u003cMongoDocument\u003e cursor = coll.find();\n       \n        List\u003cMongoDocument\u003e l = new ArrayList\u003c\u003e();\n\n        try {\n            while(cursor.hasNext()) {\n                l.add(cursor.next());\n            }\n        }finally {\n            cursor.close();\n        }\n\n        return l;\n    }\n\nMongoJack provides wrappers for the standard MongoDB api calls that will parse MongoDB documents into objects that are Jackson annotated. \n\n","funding_links":[],"categories":["Open Source"],"sub_categories":["Data Stores"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feeb%2Fdropwizard-mongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feeb%2Fdropwizard-mongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feeb%2Fdropwizard-mongo/lists"}