{"id":18429073,"url":"https://github.com/openliberty/sample-mongodb","last_synced_at":"2025-04-07T17:32:29.147Z","repository":{"id":71352091,"uuid":"167428080","full_name":"OpenLiberty/sample-mongodb","owner":"OpenLiberty","description":"Open Liberty sample application for accessing MongoDB","archived":false,"fork":false,"pushed_at":"2025-04-01T03:10:31.000Z","size":370,"stargazers_count":4,"open_issues_count":3,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-01T03:31:35.738Z","etag":null,"topics":["beanvalidation","cdi","microprofile-config","mongodb","openliberty"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenLiberty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"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}},"created_at":"2019-01-24T19:55:49.000Z","updated_at":"2025-04-01T02:37:05.000Z","dependencies_parsed_at":"2023-06-10T20:00:30.535Z","dependency_job_id":"e542ee79-0802-4e0c-9e79-201a2ed4b859","html_url":"https://github.com/OpenLiberty/sample-mongodb","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/OpenLiberty%2Fsample-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenLiberty","download_url":"https://codeload.github.com/OpenLiberty/sample-mongodb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247697915,"owners_count":20981271,"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":["beanvalidation","cdi","microprofile-config","mongodb","openliberty"],"created_at":"2024-11-06T05:15:42.570Z","updated_at":"2025-04-07T17:32:28.726Z","avatar_url":"https://github.com/OpenLiberty.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://github.com/OpenLiberty/open-liberty/blob/master/logos/logo_horizontal_light_navy.png)\n\n# MongoDB Sample\nThis sample shows how to store data with MongoDB using CDI and MicroProfile Config, as well as data validation with Jakarta Bean Validation.\n\n## Environment Set Up\nTo run this sample, first [download](https://github.com/OpenLiberty/sample-mongodb/archive/main.zip) or clone this repo - to clone:\n```\ngit clone git@github.com:OpenLiberty/sample-mongodb.git\n```\n\n### Setup MongoDB\nYou will also need a MongoDB instance to use this sample. If you have Docker installed, you can use the following:\n\n```\ndocker run -d --name liberty_mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=sampleUser -e MONGO_INITDB_ROOT_PASSWORD=openliberty mongo:7.0.7\n```\n\nIf you don't have Docker, you can install MongoDB manually from [mongodb.com](https://docs.mongodb.com/manual/administration/install-community/)\n\nNext, you will need to create a user for authentication. Issue the following commands from the command line: \n\nIf you're using docker, you can skip this step.\n\n```\nmongosh\nuse testdb\ndb.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'admin'}]})\n```\n\nYou should see the following:\n```\n{ ok: 1 }\n```\nNow you are ready to run the sample. Type `exit` to get out of the mongo shell.\n\n## Running the Sample\nFrom inside the sample-mongodb directory, build and start the application in Open Liberty with the following command:\n```\n./mvnw liberty:dev\n```\n\nOnce the server has started, the application is available at http://localhost:9080\n\n### Try it out\nGive the sample a try by registering a crew member. Enter a name (a String), an ID Number (an Integer), and select a Rank from the menu, then click 'Register Crew Member'.\n\nTwo more boxes will appear, one with your crew members (which you can click to remove) and one showing how your data looks in MongoDB.\n\n### Stop MongoDB\nIf you started MongoDB using docker, you can stop the container with:\n```\ndocker stop liberty_mongo\n```\n\n### How it works\nThis application uses a CDI producer ([MongoProducer.java](https://github.com/OpenLiberty/sample-mongodb/tree/master/src/main/java/io/openliberty/sample/mongo/MongoProducer.java)) to inject a MongoDatabase. For more info on using a CDI producer with MongoDB, check out this [blog post](https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html). It provides access to the database in a RESTful manner in [CrewService.java](https://github.com/OpenLiberty/sample-mongodb/tree/master/src/main/java/io/openliberty/sample/application/CrewService.java) using the `/db/crew` endpoint.\n\nCalling `POST /{id}` on the endpoint uses [Bean Validation](https://openliberty.io/guides/bean-validation.html) to validate the data we receive from the front end. [CrewMember.java](https://github.com/OpenLiberty/sample-mongodb/tree/master/src/main/java/io/openliberty/sample/application/CrewMember.java) shows the constraints as well as the messages we return to the user if those constraints aren't met.\n```java\n@NotEmpty(message = \"All crew members must have a name!\")\nprivate String name;\n\n@Pattern(regexp = \"(Captain|Officer|Engineer)\",  message = \"Crew member must be one of the listed ranks!\")\nprivate String rank;\n\n@Pattern(regexp = \"^\\\\d+$\", message = \"ID Number must be a non-negative integer!\")\nprivate String crewID; \n```\nAfter validation, we use the injected MongoDatabase to insert a new document with the crew member's information:\n```java\nMongoCollection\u003cDocument\u003e crew = db.getCollection(\"Crew\");\nDocument newCrewMember = new Document();\nnewCrewMember.put(\"Name\",crewMember.getName());\nnewCrewMember.put(\"Rank\",crewMember.getRank());\nnewCrewMember.put(\"CrewID\",crewMember.getCrewID());\ncrew.insertOne(newCrewMember);\n```\n\nCalling `DELETE /{id}` on the endpoint deletes a document corresponding to the path parameter {id}\n```java\ncrew.deleteOne(new Document(\"_id\", new ObjectId(id))); \n```\nCalling `GET` on the endpoint retrieves the data and does some formatting for the front end.\n```java\nMongoCollection\u003cDocument\u003e crew = db.getCollection(\"Crew\");\nfor (Document d : crew.find()) {\n\tsb.append(d.toJson());\n}\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenliberty%2Fsample-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenliberty%2Fsample-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenliberty%2Fsample-mongodb/lists"}